2011-10-14 14:23:23 +00:00
|
|
|
//
|
|
|
|
// File-system system calls.
|
|
|
|
// Mostly argument checking, since we don't trust
|
|
|
|
// user code, and calls into file.c and fs.c.
|
|
|
|
//
|
|
|
|
|
2006-09-06 18:19:11 +00:00
|
|
|
#include "types.h"
|
2019-05-31 16:43:20 +00:00
|
|
|
#include "riscv.h"
|
2007-08-27 23:26:33 +00:00
|
|
|
#include "defs.h"
|
2006-09-06 18:19:11 +00:00
|
|
|
#include "param.h"
|
2007-08-27 23:26:33 +00:00
|
|
|
#include "stat.h"
|
2019-07-02 13:14:47 +00:00
|
|
|
#include "spinlock.h"
|
2006-09-06 18:19:11 +00:00
|
|
|
#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"
|
2006-09-06 18:19:11 +00:00
|
|
|
#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-06 18:19:11 +00:00
|
|
|
{
|
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;
|
2017-01-31 22:47:16 +00:00
|
|
|
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;
|
2006-09-06 18:19:11 +00:00
|
|
|
return 0;
|
2006-09-07 14:13:26 +00:00
|
|
|
}
|
2006-09-06 18:19:11 +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;
|
|
|
|
}
|
|
|
|
}
|
2006-09-06 18:19:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2007-08-22 06:01:32 +00:00
|
|
|
sys_read(void)
|
2006-09-06 18:19:11 +00:00
|
|
|
{
|
2007-08-22 06:01:32 +00:00
|
|
|
struct file *f;
|
|
|
|
int n;
|
2019-06-01 09:33:38 +00:00
|
|
|
uint64 p;
|
2006-09-06 18:19:11 +00:00
|
|
|
|
2019-07-28 10:29:37 +00:00
|
|
|
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argaddr(1, &p) < 0)
|
2006-09-06 18:19:11 +00:00
|
|
|
return -1;
|
2007-09-27 05:13:10 +00:00
|
|
|
return fileread(f, p, n);
|
2006-09-07 14:13:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2006-09-07 14:13:26 +00:00
|
|
|
sys_write(void)
|
|
|
|
{
|
|
|
|
struct file *f;
|
|
|
|
int n;
|
2019-06-01 09:33:38 +00:00
|
|
|
uint64 p;
|
2006-09-06 18:19:11 +00:00
|
|
|
|
2019-07-28 10:29:37 +00:00
|
|
|
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argaddr(1, &p) < 0)
|
2006-09-07 14:13:26 +00:00
|
|
|
return -1;
|
2019-06-01 09:33:38 +00:00
|
|
|
|
2007-09-27 05:13:10 +00:00
|
|
|
return filewrite(f, p, n);
|
2006-09-06 18:19:11 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2006-09-06 18:19:11 +00:00
|
|
|
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)
|
2006-09-06 18:19:11 +00:00
|
|
|
return -1;
|
2017-01-31 22:47:16 +00:00
|
|
|
myproc()->ofile[fd] = 0;
|
2006-09-07 14:13:26 +00:00
|
|
|
fileclose(f);
|
2006-09-06 18:19:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2007-08-24 22:17:54 +00:00
|
|
|
sys_fstat(void)
|
|
|
|
{
|
|
|
|
struct file *f;
|
2019-06-01 09:33:38 +00:00
|
|
|
uint64 st; // user pointer to struct stat
|
2015-09-27 03:10:25 +00:00
|
|
|
|
2019-07-28 10:29:37 +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);
|
|
|
|
}
|
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
// Create the path new as a link to the same inode as old.
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2007-08-22 06:01:32 +00:00
|
|
|
sys_link(void)
|
|
|
|
{
|
2019-06-01 09:33:38 +00:00
|
|
|
char name[DIRSIZ], new[MAXPATH], old[MAXPATH];
|
2007-08-22 06:01:32 +00:00
|
|
|
struct inode *dp, *ip;
|
|
|
|
|
2019-06-01 09:33:38 +00:00
|
|
|
if(argstr(0, old, MAXPATH) < 0 || argstr(1, new, MAXPATH) < 0)
|
2007-08-22 06:01:32 +00:00
|
|
|
return -1;
|
2011-08-15 16:44:20 +00:00
|
|
|
|
2014-08-27 21:15:30 +00:00
|
|
|
begin_op();
|
2014-08-04 17:06:48 +00:00
|
|
|
if((ip = namei(old)) == 0){
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2014-08-04 17:06:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-08-15 16:44:20 +00:00
|
|
|
|
2007-08-24 20:54:23 +00:00
|
|
|
ilock(ip);
|
2007-08-22 06:01:32 +00:00
|
|
|
if(ip->type == T_DIR){
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(ip);
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2007-08-22 06:01:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-08-12 13:25:39 +00:00
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
ip->nlink++;
|
|
|
|
iupdate(ip);
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlock(ip);
|
|
|
|
|
|
|
|
if((dp = nameiparent(new, name)) == 0)
|
2009-05-31 02:07:51 +00:00
|
|
|
goto bad;
|
2007-08-24 20:54:23 +00:00
|
|
|
ilock(dp);
|
2009-05-31 02:07:51 +00:00
|
|
|
if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){
|
|
|
|
iunlockput(dp);
|
2007-08-24 20:54:23 +00:00
|
|
|
goto bad;
|
2009-05-31 02:07:51 +00:00
|
|
|
}
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(dp);
|
|
|
|
iput(ip);
|
2011-08-12 13:25:39 +00:00
|
|
|
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2011-08-12 13:25:39 +00:00
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
return 0;
|
2007-08-24 20:54:23 +00:00
|
|
|
|
|
|
|
bad:
|
|
|
|
ilock(ip);
|
|
|
|
ip->nlink--;
|
|
|
|
iupdate(ip);
|
|
|
|
iunlockput(ip);
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2007-08-24 20:54:23 +00:00
|
|
|
return -1;
|
2007-08-22 06:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)){
|
2019-06-04 09:57:47 +00:00
|
|
|
if(readi(dp, 0, (uint64)&de, off, sizeof(de)) != sizeof(de))
|
2007-08-22 06:01:32 +00:00
|
|
|
panic("isdirempty: readi");
|
|
|
|
if(de.inum != 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2007-08-22 06:01:32 +00:00
|
|
|
sys_unlink(void)
|
|
|
|
{
|
|
|
|
struct inode *ip, *dp;
|
|
|
|
struct dirent de;
|
2019-06-01 09:33:38 +00:00
|
|
|
char name[DIRSIZ], path[MAXPATH];
|
2007-08-22 06:01:32 +00:00
|
|
|
uint off;
|
|
|
|
|
2019-06-01 09:33:38 +00:00
|
|
|
if(argstr(0, path, MAXPATH) < 0)
|
2007-08-22 06:01:32 +00:00
|
|
|
return -1;
|
2011-08-15 16:44:20 +00:00
|
|
|
|
2014-08-27 21:15:30 +00:00
|
|
|
begin_op();
|
2014-08-04 17:06:48 +00:00
|
|
|
if((dp = nameiparent(path, name)) == 0){
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2014-08-04 17:06:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-08-15 16:44:20 +00:00
|
|
|
|
2007-08-24 20:54:23 +00:00
|
|
|
ilock(dp);
|
2007-08-22 06:01:32 +00:00
|
|
|
|
|
|
|
// Cannot unlink "." or "..".
|
2011-09-02 19:20:27 +00:00
|
|
|
if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0)
|
|
|
|
goto bad;
|
2007-08-22 06:01:32 +00:00
|
|
|
|
2011-09-02 19:20:27 +00:00
|
|
|
if((ip = dirlookup(dp, name, &off)) == 0)
|
|
|
|
goto bad;
|
2007-08-24 20:54:23 +00:00
|
|
|
ilock(ip);
|
2007-08-22 06:01:32 +00:00
|
|
|
|
|
|
|
if(ip->nlink < 1)
|
|
|
|
panic("unlink: nlink < 1");
|
|
|
|
if(ip->type == T_DIR && !isdirempty(ip)){
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(ip);
|
2011-09-02 19:20:27 +00:00
|
|
|
goto bad;
|
2007-08-22 06:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&de, 0, sizeof(de));
|
2019-06-04 09:57:47 +00:00
|
|
|
if(writei(dp, 0, (uint64)&de, off, sizeof(de)) != sizeof(de))
|
2007-08-22 06:01:32 +00:00
|
|
|
panic("unlink: writei");
|
2008-10-17 12:42:13 +00:00
|
|
|
if(ip->type == T_DIR){
|
|
|
|
dp->nlink--;
|
|
|
|
iupdate(dp);
|
|
|
|
}
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(dp);
|
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
ip->nlink--;
|
|
|
|
iupdate(ip);
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(ip);
|
2011-08-12 13:25:39 +00:00
|
|
|
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2011-08-12 13:25:39 +00:00
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
return 0;
|
2011-09-02 19:20:27 +00:00
|
|
|
|
|
|
|
bad:
|
|
|
|
iunlockput(dp);
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2011-09-02 19:20:27 +00:00
|
|
|
return -1;
|
2007-08-22 06:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct inode*
|
2009-05-31 02:07:51 +00:00
|
|
|
create(char *path, short type, short major, short minor)
|
2007-08-22 06:01:32 +00:00
|
|
|
{
|
|
|
|
struct inode *ip, *dp;
|
|
|
|
char name[DIRSIZ];
|
|
|
|
|
2007-08-24 20:54:23 +00:00
|
|
|
if((dp = nameiparent(path, name)) == 0)
|
2007-08-22 06:01:32 +00:00
|
|
|
return 0;
|
2007-08-24 20:54:23 +00:00
|
|
|
ilock(dp);
|
2007-08-22 06:01:32 +00:00
|
|
|
|
2019-03-21 01:15:38 +00:00
|
|
|
if((ip = dirlookup(dp, name, 0)) != 0){
|
2007-08-24 20:54:23 +00:00
|
|
|
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);
|
2007-08-22 06:01:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-07-12 02:26:01 +00:00
|
|
|
|
|
|
|
if((ip = ialloc(dp->dev, type)) == 0)
|
|
|
|
panic("create: ialloc");
|
|
|
|
|
2007-08-24 20:54:23 +00:00
|
|
|
ilock(ip);
|
2007-08-22 06:01:32 +00:00
|
|
|
ip->major = major;
|
|
|
|
ip->minor = minor;
|
2007-08-27 16:06:15 +00:00
|
|
|
ip->nlink = 1;
|
2007-08-22 06:01:32 +00:00
|
|
|
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");
|
2007-08-22 06:01:32 +00:00
|
|
|
}
|
2008-10-16 15:18:49 +00:00
|
|
|
|
2009-05-31 02:07:51 +00:00
|
|
|
if(dirlink(dp, name, ip->inum) < 0)
|
|
|
|
panic("create: dirlink");
|
2008-10-16 15:18:49 +00:00
|
|
|
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(dp);
|
2011-08-12 13:25:39 +00:00
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2006-09-06 18:19:11 +00:00
|
|
|
sys_open(void)
|
|
|
|
{
|
2019-06-01 09:33:38 +00:00
|
|
|
char path[MAXPATH];
|
2007-08-21 19:22:08 +00:00
|
|
|
int fd, omode;
|
2006-09-07 14:13:26 +00:00
|
|
|
struct file *f;
|
2007-08-21 19:22:08 +00:00
|
|
|
struct inode *ip;
|
2006-09-06 18:19:11 +00:00
|
|
|
|
2019-06-01 09:33:38 +00:00
|
|
|
if(argstr(0, path, MAXPATH) < 0 || argint(1, &omode) < 0)
|
2006-09-06 18:19:11 +00:00
|
|
|
return -1;
|
2014-08-04 17:06:48 +00:00
|
|
|
|
2014-08-27 21:15:30 +00:00
|
|
|
begin_op();
|
2014-08-04 17:06:48 +00:00
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
if(omode & O_CREATE){
|
2011-08-12 13:25:39 +00:00
|
|
|
ip = create(path, T_FILE, 0, 0);
|
2014-08-04 17:06:48 +00:00
|
|
|
if(ip == 0){
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2007-08-22 06:01:32 +00:00
|
|
|
return -1;
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
2007-08-28 18:37:41 +00:00
|
|
|
} else {
|
2014-08-04 17:06:48 +00:00
|
|
|
if((ip = namei(path)) == 0){
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2007-08-22 06:01:32 +00:00
|
|
|
return -1;
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
2007-08-24 20:54:23 +00:00
|
|
|
ilock(ip);
|
2009-05-31 02:07:51 +00:00
|
|
|
if(ip->type == T_DIR && omode != O_RDONLY){
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(ip);
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2007-08-22 06:01:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-09-06 18:19:11 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
|
|
|
|
if(f)
|
|
|
|
fileclose(f);
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(ip);
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2006-09-06 18:19:11 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-08-24 20:54:23 +00:00
|
|
|
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;
|
2006-09-06 18:19:11 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2009-07-12 02:26:01 +00:00
|
|
|
sys_mkdir(void)
|
|
|
|
{
|
2019-06-01 09:33:38 +00:00
|
|
|
char path[MAXPATH];
|
2009-07-12 02:26:01 +00:00
|
|
|
struct inode *ip;
|
|
|
|
|
2014-08-27 21:15:30 +00:00
|
|
|
begin_op();
|
2019-06-01 09:33:38 +00:00
|
|
|
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;
|
2011-08-12 13:25:39 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2006-09-06 18:19:11 +00:00
|
|
|
sys_mknod(void)
|
|
|
|
{
|
2007-08-22 06:01:32 +00:00
|
|
|
struct inode *ip;
|
2019-06-01 09:33:38 +00:00
|
|
|
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();
|
2019-06-01 09:33:38 +00:00
|
|
|
if((argstr(0, path, MAXPATH)) < 0 ||
|
2007-08-24 20:54:23 +00:00
|
|
|
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();
|
2006-09-06 18:19:11 +00:00
|
|
|
return -1;
|
2011-08-12 13:25:39 +00:00
|
|
|
}
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(ip);
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2006-09-07 14:13:26 +00:00
|
|
|
return 0;
|
2006-09-06 18:19:11 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2006-09-06 18:19:11 +00:00
|
|
|
sys_chdir(void)
|
|
|
|
{
|
2019-06-01 09:33:38 +00:00
|
|
|
char path[MAXPATH];
|
2007-08-22 06:01:32 +00:00
|
|
|
struct inode *ip;
|
2019-05-31 13:45:59 +00:00
|
|
|
struct proc *p = myproc();
|
2017-02-01 01:21:14 +00:00
|
|
|
|
2014-08-27 21:15:30 +00:00
|
|
|
begin_op();
|
2019-06-01 09:33:38 +00:00
|
|
|
if(argstr(0, path, MAXPATH) < 0 || (ip = namei(path)) == 0){
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2006-09-06 18:19:11 +00:00
|
|
|
return -1;
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
2007-08-24 20:54:23 +00:00
|
|
|
ilock(ip);
|
2007-08-28 18:32:08 +00:00
|
|
|
if(ip->type != T_DIR){
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(ip);
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2006-09-06 18:19:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-24 20:54:23 +00:00
|
|
|
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;
|
2006-09-06 18:19:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2006-09-06 18:19:11 +00:00
|
|
|
sys_exec(void)
|
|
|
|
{
|
2019-06-01 09:33:38 +00:00
|
|
|
char path[MAXPATH], *argv[MAXARG];
|
2007-08-21 19:22:08 +00:00
|
|
|
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
|
|
|
|
2019-06-01 09:33:38 +00:00
|
|
|
if(argstr(0, path, MAXPATH) < 0 || argaddr(1, &uargv) < 0){
|
2006-09-06 18:19:11 +00:00
|
|
|
return -1;
|
2010-07-02 18:51:53 +00:00
|
|
|
}
|
2007-08-22 06:01:32 +00:00
|
|
|
memset(argv, 0, sizeof(argv));
|
2007-08-21 19:22:08 +00:00
|
|
|
for(i=0;; i++){
|
2019-05-31 16:43:20 +00:00
|
|
|
if(i >= NELEM(argv)){
|
2007-08-21 19:22:08 +00:00
|
|
|
return -1;
|
2019-05-31 16:43:20 +00:00
|
|
|
}
|
|
|
|
if(fetchaddr(uargv+sizeof(uint64)*i, (uint64*)&uarg) < 0){
|
2007-08-21 19:22:08 +00:00
|
|
|
return -1;
|
2019-05-31 16:43:20 +00:00
|
|
|
}
|
2007-08-21 19:22:08 +00:00
|
|
|
if(uarg == 0){
|
|
|
|
argv[i] = 0;
|
2006-09-06 18:19:11 +00:00
|
|
|
break;
|
2007-08-21 19:22:08 +00:00
|
|
|
}
|
2019-06-01 09:33:38 +00:00
|
|
|
argv[i] = kalloc();
|
|
|
|
if(argv[i] == 0)
|
|
|
|
panic("sys_exec kalloc");
|
|
|
|
if(fetchstr(uarg, argv[i], PGSIZE) < 0){
|
2007-08-21 19:22:08 +00:00
|
|
|
return -1;
|
2019-05-31 16:43:20 +00:00
|
|
|
}
|
2006-09-06 18:19:11 +00:00
|
|
|
}
|
2019-06-01 09:33:38 +00:00
|
|
|
|
|
|
|
int ret = exec(path, argv);
|
|
|
|
|
|
|
|
for(i = 0; i < NELEM(argv) && argv[i] != 0; i++)
|
|
|
|
kfree(argv[i]);
|
|
|
|
|
|
|
|
return ret;
|
2006-09-06 18:19:11 +00:00
|
|
|
}
|
2007-08-21 19:22:08 +00:00
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
uint64
|
2007-08-22 06:01:32 +00:00
|
|
|
sys_pipe(void)
|
|
|
|
{
|
2019-06-01 09:33:38 +00:00
|
|
|
uint64 fdarray; // user pointer to array of two integers
|
2007-08-22 06:01:32 +00:00
|
|
|
struct file *rf, *wf;
|
|
|
|
int fd0, fd1;
|
2019-06-01 09:33:38 +00:00
|
|
|
struct proc *p = myproc();
|
2007-08-22 06:01:32 +00:00
|
|
|
|
2019-07-28 10:29:37 +00:00
|
|
|
if(argaddr(0, &fdarray) < 0)
|
2007-08-22 06:01:32 +00:00
|
|
|
return -1;
|
2007-08-28 04:22:35 +00:00
|
|
|
if(pipealloc(&rf, &wf) < 0)
|
2007-08-22 06:01:32 +00:00
|
|
|
return -1;
|
|
|
|
fd0 = -1;
|
|
|
|
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
|
|
|
|
if(fd0 >= 0)
|
2019-06-01 09:33:38 +00:00
|
|
|
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;
|
2007-08-22 06:01:32 +00:00
|
|
|
fileclose(rf);
|
|
|
|
fileclose(wf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|