提交 224f6598 创建 作者: rsc's avatar rsc

refactor syscall code

上级 31085bb4
...@@ -16,7 +16,7 @@ struct jmpbuf; ...@@ -16,7 +16,7 @@ struct jmpbuf;
void setupsegs(struct proc*); void setupsegs(struct proc*);
struct proc* copyproc(struct proc*); struct proc* copyproc(struct proc*);
struct spinlock; struct spinlock;
uint growproc(int); int growproc(int);
void sleep(void*, struct spinlock*); void sleep(void*, struct spinlock*);
void wakeup(void*); void wakeup(void*);
void scheduler(void); void scheduler(void);
...@@ -43,10 +43,10 @@ int strncmp(const char*, const char*, uint); ...@@ -43,10 +43,10 @@ int strncmp(const char*, const char*, uint);
// syscall.c // syscall.c
void syscall(void); void syscall(void);
int fetchint(struct proc*, uint, int*); int fetchint(struct proc*, uint, int*);
int fetchbyte(struct proc*, uint, char*); int fetchstr(struct proc*, uint, char**);
int fetcharg(int, void*); int argint(int, int*);
int checkstring(uint); int argptr(int, char**, int);
int putint(struct proc*, uint, int); int argstr(int, char**);
// picirq.c // picirq.c
extern ushort irq_mask_8259A; extern ushort irq_mask_8259A;
...@@ -99,7 +99,6 @@ int pipe_read(struct pipe*, char*, int); ...@@ -99,7 +99,6 @@ int pipe_read(struct pipe*, char*, int);
// file.c // file.c
struct stat; struct stat;
void fileinit(void); void fileinit(void);
int fdalloc(void);
struct file* filealloc(void); struct file* filealloc(void);
void fileclose(struct file*); void fileclose(struct file*);
int fileread(struct file*, char*, int n); int fileread(struct file*, char*, int n);
......
...@@ -22,19 +22,7 @@ fileinit(void) ...@@ -22,19 +22,7 @@ fileinit(void)
initlock(&fd_table_lock, "fd_table"); initlock(&fd_table_lock, "fd_table");
} }
// Allocate a file descriptor number for curproc. // Allocate a file structure
int
fdalloc(void)
{
int fd;
struct proc *p = curproc[cpu()];
for(fd = 0; fd < NOFILE; fd++)
if(p->ofile[fd] == 0)
return fd;
return -1;
}
// Allocate a file descriptor structure
struct file* struct file*
filealloc(void) filealloc(void)
{ {
......
...@@ -21,64 +21,71 @@ ...@@ -21,64 +21,71 @@
// library system call function. The saved user %esp points // library system call function. The saved user %esp points
// to a saved program counter, and then the first argument. // to a saved program counter, and then the first argument.
// Fetch 32 bits from a user-supplied pointer. // Fetch the int at addr from process p.
// Returns 0 if addr was OK, -1 if illegal.
int int
fetchint(struct proc *p, uint addr, int *ip) fetchint(struct proc *p, uint addr, int *ip)
{ {
*ip = 0; if(addr >= p->sz || addr+4 > p->sz)
if(addr > p->sz - 4)
return -1; return -1;
*ip = *(int*)(p->mem + addr); *ip = *(int*)(p->mem + addr);
return 0; return 0;
} }
// Fetch byte from a user-supplied pointer. // Fetch the nul-terminated string at addr from process p.
// Returns 0 on success, -1 if pointer is illegal. // Doesn't actually copy the string - just sets *pp to point at it.
// Returns length of string, not including nul.
int int
fetchbyte(struct proc *p, uint addr, char *c) fetchstr(struct proc *p, uint addr, char **pp)
{ {
char *cp, *ep;
if(addr >= p->sz) if(addr >= p->sz)
return -1; return -1;
*c = *(p->mem + addr); *pp = p->mem + addr;
return 0; ep = p->mem + p->sz;
for(cp = *pp; cp < ep; cp++)
if(*cp == 0)
return cp - *pp;
return -1;
} }
// Fetch the argno'th word-sized system call argument as an integer.
int int
fetcharg(int argno, void *ip) argint(int argno, int *ip)
{ {
uint esp; struct proc *p = curproc[cpu()];
esp = (uint) curproc[cpu()]->tf->esp; return fetchint(p, p->tf->esp + 4 + 4*argno, ip);
return fetchint(curproc[cpu()], esp + 4 + 4*argno, ip);
} }
// Check that an entire string is valid in user space. // Fetch the nth word-sized system call argument as a pointer
// Returns the length, not including null, or -1. // to a block of memory of size n bytes. Check that the pointer
// lies within the process address space.
int int
checkstring(uint s) argptr(int argno, char **pp, int size)
{ {
char c; int i;
int len = 0; struct proc *p = curproc[cpu()];
for(;;){ if(argint(argno, &i) < 0)
if(fetchbyte(curproc[cpu()], s, &c) < 0) return -1;
return -1; if((uint)i >= p->sz || (uint)i+size >= p->sz)
if(c == '\0') return -1;
return len; *pp = p->mem + i;
len++; return 0;
s++;
}
} }
// Fetch the nth word-sized system call argument as a string pointer.
// Check that the pointer is valid and the string is nul-terminated.
// (There is no shared writable memory, so the string can't change
// between this check and being used by the kernel.)
int int
putint(struct proc *p, uint addr, int x) argstr(int argno, char **pp)
{ {
if(addr > p->sz - 4) int addr;
if(argint(argno, &addr) < 0)
return -1; return -1;
memmove(p->mem + addr, &x, 4); return fetchstr(curproc[cpu()], addr, pp);
return 0;
} }
extern int sys_chdir(void); extern int sys_chdir(void);
......
// System call numbers
#define SYS_fork 1 #define SYS_fork 1
#define SYS_exit 2 #define SYS_exit 2
#define SYS_wait 3 #define SYS_wait 3
......
差异被折叠。
...@@ -44,7 +44,7 @@ sys_kill(void) ...@@ -44,7 +44,7 @@ sys_kill(void)
{ {
int pid; int pid;
if(fetcharg(0, &pid) < 0) if(argint(0, &pid) < 0)
return -1; return -1;
return proc_kill(pid); return proc_kill(pid);
} }
...@@ -52,20 +52,19 @@ sys_kill(void) ...@@ -52,20 +52,19 @@ sys_kill(void)
int int
sys_getpid(void) sys_getpid(void)
{ {
struct proc *cp = curproc[cpu()]; return curproc[cpu()]->pid;
return cp->pid;
} }
int int
sys_sbrk(void) sys_sbrk(void)
{ {
uint addr; int addr;
int n; int n;
struct proc *cp = curproc[cpu()]; struct proc *cp = curproc[cpu()];
if(fetcharg(0, &n) < 0) if(argint(0, &n) < 0)
return -1; return -1;
if((addr = growproc(n)) == 0xffffffff) if((addr = growproc(n)) < 0)
return -1; return -1;
setupsegs(cp); setupsegs(cp);
return addr; return addr;
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论