Skip to content

Commit

Permalink
refactor syscall code
Browse files Browse the repository at this point in the history
  • Loading branch information
rsc committed Sep 7, 2006
1 parent 31085bb commit 224f659
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 243 deletions.
11 changes: 5 additions & 6 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct jmpbuf;
void setupsegs(struct proc*);
struct proc* copyproc(struct proc*);
struct spinlock;
uint growproc(int);
int growproc(int);
void sleep(void*, struct spinlock*);
void wakeup(void*);
void scheduler(void);
Expand All @@ -43,10 +43,10 @@ int strncmp(const char*, const char*, uint);
// syscall.c
void syscall(void);
int fetchint(struct proc*, uint, int*);
int fetchbyte(struct proc*, uint, char*);
int fetcharg(int, void*);
int checkstring(uint);
int putint(struct proc*, uint, int);
int fetchstr(struct proc*, uint, char**);
int argint(int, int*);
int argptr(int, char**, int);
int argstr(int, char**);

// picirq.c
extern ushort irq_mask_8259A;
Expand Down Expand Up @@ -99,7 +99,6 @@ int pipe_read(struct pipe*, char*, int);
// file.c
struct stat;
void fileinit(void);
int fdalloc(void);
struct file* filealloc(void);
void fileclose(struct file*);
int fileread(struct file*, char*, int n);
Expand Down
14 changes: 1 addition & 13 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,7 @@ fileinit(void)
initlock(&fd_table_lock, "fd_table");
}

// Allocate a file descriptor number for curproc.
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
// Allocate a file structure
struct file*
filealloc(void)
{
Expand Down
71 changes: 39 additions & 32 deletions syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,71 @@
// library system call function. The saved user %esp points
// to a saved program counter, and then the first argument.

// Fetch 32 bits from a user-supplied pointer.
// Returns 0 if addr was OK, -1 if illegal.
// Fetch the int at addr from process p.
int
fetchint(struct proc *p, uint addr, int *ip)
{
*ip = 0;

if(addr > p->sz - 4)
if(addr >= p->sz || addr+4 > p->sz)
return -1;
*ip = *(int*)(p->mem + addr);
return 0;
}

// Fetch byte from a user-supplied pointer.
// Returns 0 on success, -1 if pointer is illegal.
// Fetch the nul-terminated string at addr from process p.
// Doesn't actually copy the string - just sets *pp to point at it.
// Returns length of string, not including nul.
int
fetchbyte(struct proc *p, uint addr, char *c)
fetchstr(struct proc *p, uint addr, char **pp)
{
char *cp, *ep;

if(addr >= p->sz)
return -1;
*c = *(p->mem + addr);
return 0;
*pp = p->mem + addr;
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
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(curproc[cpu()], esp + 4 + 4*argno, ip);
return fetchint(p, p->tf->esp + 4 + 4*argno, ip);
}

// Check that an entire string is valid in user space.
// Returns the length, not including null, or -1.
// Fetch the nth word-sized system call argument as a pointer
// to a block of memory of size n bytes. Check that the pointer
// lies within the process address space.
int
checkstring(uint s)
argptr(int argno, char **pp, int size)
{
char c;
int len = 0;

for(;;){
if(fetchbyte(curproc[cpu()], s, &c) < 0)
return -1;
if(c == '\0')
return len;
len++;
s++;
}
int i;
struct proc *p = curproc[cpu()];

if(argint(argno, &i) < 0)
return -1;
if((uint)i >= p->sz || (uint)i+size >= p->sz)
return -1;
*pp = p->mem + i;
return 0;
}

// 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
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;
memmove(p->mem + addr, &x, 4);
return 0;
return fetchstr(curproc[cpu()], addr, pp);
}

extern int sys_chdir(void);
Expand Down
1 change: 1 addition & 0 deletions syscall.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// System call numbers
#define SYS_fork 1
#define SYS_exit 2
#define SYS_wait 3
Expand Down
Loading

0 comments on commit 224f659

Please sign in to comment.