xv6-65oo2/kernel/sysfile.c

484 lines
8.2 KiB
C
Raw Normal View History

//
// File-system system calls.
// Mostly argument checking, since we don't trust
// user code, and calls into file.c and fs.c.
//
#include "types.h"
#include "riscv.h"
2007-08-27 23:26:33 +00:00
#include "defs.h"
#include "param.h"
2007-08-27 23:26:33 +00:00
#include "stat.h"
#include "spinlock.h"
#include "proc.h"
#include "fs.h"
2016-09-12 00:59:57 +00:00
#include "sleeplock.h"
2006-09-06 18:40:28 +00:00
#include "file.h"
#include "fcntl.h"
2006-09-07 14:13:26 +00:00
// Fetch the nth word-sized system call argument as a file descriptor
// and return both the descriptor and the corresponding struct file.
static int
2007-08-27 23:53:17 +00:00
argfd(int n, int *pfd, struct file **pf)
{
2006-09-07 14:13:26 +00:00
int fd;
struct file *f;
2007-08-27 23:53:17 +00:00
if(argint(n, &fd) < 0)
2006-09-07 14:13:26 +00:00
return -1;
if(fd < 0 || fd >= NOFILE || (f=myproc()->ofile[fd]) == 0)
2006-09-07 14:13:26 +00:00
return -1;
if(pfd)
*pfd = fd;
if(pf)
*pf = f;
return 0;
2006-09-07 14:13:26 +00:00
}
2006-09-07 14:13:26 +00:00
// Allocate a file descriptor for the given file.
// Takes over file reference from caller on success.
static int
fdalloc(struct file *f)
{
int fd;
2019-05-31 13:45:59 +00:00
struct proc *p = myproc();
2007-08-09 17:32:40 +00:00
2006-09-07 14:13:26 +00:00
for(fd = 0; fd < NOFILE; fd++){
2019-05-31 13:45:59 +00:00
if(p->ofile[fd] == 0){
p->ofile[fd] = f;
2006-09-07 14:13:26 +00:00
return fd;
}
}
return -1;
}
uint64
2009-07-12 02:26:01 +00:00
sys_dup(void)
{
struct file *f;
int fd;
2015-09-27 03:10:25 +00:00
2009-07-12 02:26:01 +00:00
if(argfd(0, 0, &f) < 0)
return -1;
if((fd=fdalloc(f)) < 0)
return -1;
filedup(f);
return fd;
}
uint64
sys_read(void)
{
struct file *f;
int n;
uint64 p;
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argaddr(1, &p) < 0)
return -1;
return fileread(f, p, n);
2006-09-07 14:13:26 +00:00
}
uint64
2006-09-07 14:13:26 +00:00
sys_write(void)
{
struct file *f;
int n;
uint64 p;
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argaddr(1, &p) < 0)
2006-09-07 14:13:26 +00:00
return -1;
return filewrite(f, p, n);
}
uint64
sys_close(void)
{
int fd;
2006-09-07 14:13:26 +00:00
struct file *f;
2015-09-27 03:10:25 +00:00
2006-09-07 14:13:26 +00:00
if(argfd(0, &fd, &f) < 0)
return -1;
myproc()->ofile[fd] = 0;
2006-09-07 14:13:26 +00:00
fileclose(f);
return 0;
}
uint64
2007-08-24 22:17:54 +00:00
sys_fstat(void)
{
struct file *f;
uint64 st; // user pointer to struct stat
2015-09-27 03:10:25 +00:00
if(argfd(0, 0, &f) < 0 || argaddr(1, &st) < 0)
2007-08-24 22:17:54 +00:00
return -1;
return filestat(f, st);
}
// Create the path new as a link to the same inode as old.
uint64
sys_link(void)
{
char name[DIRSIZ], new[MAXPATH], old[MAXPATH];
struct inode *dp, *ip;
if(argstr(0, old, MAXPATH) < 0 || argstr(1, new, MAXPATH) < 0)
return -1;
2014-08-27 21:15:30 +00:00
begin_op();
if((ip = namei(old)) == 0){
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
ilock(ip);
if(ip->type == T_DIR){
iunlockput(ip);
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
ip->nlink++;
iupdate(ip);
iunlock(ip);
if((dp = nameiparent(new, name)) == 0)
2009-05-31 02:07:51 +00:00
goto bad;
ilock(dp);
2009-05-31 02:07:51 +00:00
if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){
iunlockput(dp);
goto bad;
2009-05-31 02:07:51 +00:00
}
iunlockput(dp);
iput(ip);
2014-08-27 21:15:30 +00:00
end_op();
return 0;
bad:
ilock(ip);
ip->nlink--;
iupdate(ip);
iunlockput(ip);
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
// Is the directory dp empty except for "." and ".." ?
static int
isdirempty(struct inode *dp)
{
int off;
struct dirent de;
for(off=2*sizeof(de); off<dp->size; off+=sizeof(de)){
if(readi(dp, 0, (uint64)&de, off, sizeof(de)) != sizeof(de))
panic("isdirempty: readi");
if(de.inum != 0)
return 0;
}
return 1;
}
uint64
sys_unlink(void)
{
struct inode *ip, *dp;
struct dirent de;
char name[DIRSIZ], path[MAXPATH];
uint off;
if(argstr(0, path, MAXPATH) < 0)
return -1;
2014-08-27 21:15:30 +00:00
begin_op();
if((dp = nameiparent(path, name)) == 0){
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
ilock(dp);
// Cannot unlink "." or "..".
if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0)
goto bad;
if((ip = dirlookup(dp, name, &off)) == 0)
goto bad;
ilock(ip);
if(ip->nlink < 1)
panic("unlink: nlink < 1");
if(ip->type == T_DIR && !isdirempty(ip)){
iunlockput(ip);
goto bad;
}
memset(&de, 0, sizeof(de));
if(writei(dp, 0, (uint64)&de, off, sizeof(de)) != sizeof(de))
panic("unlink: writei");
if(ip->type == T_DIR){
dp->nlink--;
iupdate(dp);
}
iunlockput(dp);
ip->nlink--;
iupdate(ip);
iunlockput(ip);
2014-08-27 21:15:30 +00:00
end_op();
return 0;
bad:
iunlockput(dp);
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
static struct inode*
2009-05-31 02:07:51 +00:00
create(char *path, short type, short major, short minor)
{
struct inode *ip, *dp;
char name[DIRSIZ];
if((dp = nameiparent(path, name)) == 0)
return 0;
ilock(dp);
if((ip = dirlookup(dp, name, 0)) != 0){
iunlockput(dp);
ilock(ip);
2019-06-13 14:29:27 +00:00
if(type == T_FILE && (ip->type == T_FILE || ip->type == T_DEVICE))
2009-07-12 02:26:01 +00:00
return ip;
iunlockput(ip);
return 0;
}
2009-07-12 02:26:01 +00:00
if((ip = ialloc(dp->dev, type)) == 0)
panic("create: ialloc");
ilock(ip);
ip->major = major;
ip->minor = minor;
2007-08-27 16:06:15 +00:00
ip->nlink = 1;
iupdate(ip);
if(type == T_DIR){ // Create . and .. entries.
dp->nlink++; // for ".."
iupdate(dp);
// No ip->nlink++ for ".": avoid cyclic ref count.
if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0)
2007-08-27 14:39:50 +00:00
panic("create dots");
}
2009-05-31 02:07:51 +00:00
if(dirlink(dp, name, ip->inum) < 0)
panic("create: dirlink");
iunlockput(dp);
return ip;
}
uint64
sys_open(void)
{
char path[MAXPATH];
int fd, omode;
2006-09-07 14:13:26 +00:00
struct file *f;
struct inode *ip;
int n;
if((n = argstr(0, path, MAXPATH)) < 0 || argint(1, &omode) < 0)
return -1;
2014-08-27 21:15:30 +00:00
begin_op();
if(omode & O_CREATE){
ip = create(path, T_FILE, 0, 0);
if(ip == 0){
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
2007-08-28 18:37:41 +00:00
} else {
if((ip = namei(path)) == 0){
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
ilock(ip);
2009-05-31 02:07:51 +00:00
if(ip->type == T_DIR && omode != O_RDONLY){
iunlockput(ip);
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
}
2019-06-13 14:29:27 +00:00
if(ip->type == T_DEVICE && (ip->major < 0 || ip->major >= NDEV)){
iunlockput(ip);
end_op();
return -1;
}
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
if(f)
fileclose(f);
iunlockput(ip);
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
2019-06-13 14:29:27 +00:00
if(ip->type == T_DEVICE){
f->type = FD_DEVICE;
f->major = ip->major;
} else {
f->type = FD_INODE;
f->off = 0;
}
f->ip = ip;
2007-08-27 14:39:50 +00:00
f->readable = !(omode & O_WRONLY);
f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
2019-06-13 14:29:27 +00:00
iunlock(ip);
end_op();
2006-09-07 14:13:26 +00:00
return fd;
}
uint64
2009-07-12 02:26:01 +00:00
sys_mkdir(void)
{
char path[MAXPATH];
2009-07-12 02:26:01 +00:00
struct inode *ip;
2014-08-27 21:15:30 +00:00
begin_op();
if(argstr(0, path, MAXPATH) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0){
2014-08-27 21:15:30 +00:00
end_op();
2009-07-12 02:26:01 +00:00
return -1;
}
2009-07-12 02:26:01 +00:00
iunlockput(ip);
2014-08-27 21:15:30 +00:00
end_op();
2009-07-12 02:26:01 +00:00
return 0;
}
uint64
sys_mknod(void)
{
struct inode *ip;
char path[MAXPATH];
2007-08-24 20:59:43 +00:00
int major, minor;
2015-09-27 03:10:25 +00:00
2014-08-27 21:15:30 +00:00
begin_op();
if((argstr(0, path, MAXPATH)) < 0 ||
argint(1, &major) < 0 ||
argint(2, &minor) < 0 ||
2019-06-13 14:29:27 +00:00
(ip = create(path, T_DEVICE, major, minor)) == 0){
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
iunlockput(ip);
2014-08-27 21:15:30 +00:00
end_op();
2006-09-07 14:13:26 +00:00
return 0;
}
uint64
sys_chdir(void)
{
char path[MAXPATH];
struct inode *ip;
2019-05-31 13:45:59 +00:00
struct proc *p = myproc();
2014-08-27 21:15:30 +00:00
begin_op();
if(argstr(0, path, MAXPATH) < 0 || (ip = namei(path)) == 0){
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
ilock(ip);
2007-08-28 18:32:08 +00:00
if(ip->type != T_DIR){
iunlockput(ip);
2014-08-27 21:15:30 +00:00
end_op();
return -1;
}
iunlock(ip);
2019-05-31 13:45:59 +00:00
iput(p->cwd);
2014-08-27 21:15:30 +00:00
end_op();
2019-05-31 13:45:59 +00:00
p->cwd = ip;
return 0;
}
uint64
sys_exec(void)
{
char path[MAXPATH], *argv[MAXARG];
int i;
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times. The x86-64 doesn't just add two levels to page tables to support 64 bit addresses, but is a different processor. For example, calling conventions, system calls, and segmentation are different from 32-bit x86. Segmentation is basically gone, but gs/fs in combination with MSRs can be used to hold a per-core pointer. In general, x86-64 is more straightforward than 32-bit x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch. A summary of the changes is as follows: - Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu), because xv6's boot loader doesn't understand 64bit ELF files. And, we don't care anymore about booting. - Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img, bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than MAXFILE! - Update gdb.tmpl to be for i386 or x86-64 - Console/printf: use stdarg.h and treat 64-bit addresses different from ints (32-bit) - Update elfhdr to be 64 bit - entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page table in 32-bit mode before switching to 64-bit mode, share code for entering boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state mostly disappear.) - exec.c: fix passing argv (64-bit now instead of 32-bit). - initcode.c: use syscall instead of int. - kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of address space! - proc.c: initial return is through new syscall path instead of trapret. - proc.h: update struct cpu to have some scratch space since syscall saves less state than int, update struct context to reflect x86-64 calling conventions. - swtch: simplify for x86-64 calling conventions. - syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are passed through registers), and fetchaddr to read a 64-bit value from user space. - sysfile: update to handle pointers from user space (e.g., sys_exec), which are 64 bits. - trap.c: no special trap vector for sys calls, because x86-64 has a different plan for system calls. - trapasm: one plan for syscalls and one plan for traps (interrupt and exceptions). On x86-64, the kernel is responsible for switching user/kernel stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR GS_KERN_BASE to point to the core's cpu structure (using swapgs). - types.h: add uint64, and change pde_t to uint64 - usertests: exit() when fork fails, which helped in tracking down one of the bugs in the switch from 32-bit to 64-bit - vectors: update to make them 64 bits - vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task segment to set kernel stack for interrupts (but simpler than in 32-bit mode), add an extra argument to freevm (size of user part of address space) to avoid checking all entries till KERNBASE (there are MANY TB before the top 1TB). - x86: update trapframe to have 64-bit entries, which is what the processor pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr, which needs the gcc directives packed and aligned. TODO: - use int32 instead of int? - simplify curproc(). xv6 has per-cpu state again, but this time it must have it. - avoid repetition in walkpgdir - fix validateint() in usertests.c - fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
uint64 uargv, uarg;
2006-09-07 14:13:26 +00:00
if(argstr(0, path, MAXPATH) < 0 || argaddr(1, &uargv) < 0){
return -1;
}
memset(argv, 0, sizeof(argv));
for(i=0;; i++){
if(i >= NELEM(argv)){
goto bad;
}
if(fetchaddr(uargv+sizeof(uint64)*i, (uint64*)&uarg) < 0){
goto bad;
}
if(uarg == 0){
argv[i] = 0;
break;
}
argv[i] = kalloc();
if(argv[i] == 0)
panic("sys_exec kalloc");
if(fetchstr(uarg, argv[i], PGSIZE) < 0){
goto bad;
}
}
int ret = exec(path, argv);
for(i = 0; i < NELEM(argv) && argv[i] != 0; i++)
kfree(argv[i]);
return ret;
bad:
for(i = 0; i < NELEM(argv) && argv[i] != 0; i++)
kfree(argv[i]);
return -1;
}
uint64
sys_pipe(void)
{
uint64 fdarray; // user pointer to array of two integers
struct file *rf, *wf;
int fd0, fd1;
struct proc *p = myproc();
if(argaddr(0, &fdarray) < 0)
return -1;
2007-08-28 04:22:35 +00:00
if(pipealloc(&rf, &wf) < 0)
return -1;
fd0 = -1;
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
if(fd0 >= 0)
p->ofile[fd0] = 0;
fileclose(rf);
fileclose(wf);
return -1;
}
if(copyout(p->pagetable, fdarray, (char*)&fd0, sizeof(fd0)) < 0 ||
copyout(p->pagetable, fdarray+sizeof(fd0), (char *)&fd1, sizeof(fd1)) < 0){
p->ofile[fd0] = 0;
p->ofile[fd1] = 0;
fileclose(rf);
fileclose(wf);
return -1;
}
return 0;
}