2006-06-15 18:02:20 +02:00
|
|
|
#include "types.h"
|
2006-08-12 06:33:50 +02:00
|
|
|
#include "stat.h"
|
2006-06-15 18:02:20 +02:00
|
|
|
#include "param.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "x86.h"
|
|
|
|
#include "traps.h"
|
|
|
|
#include "syscall.h"
|
2006-07-12 03:48:35 +02:00
|
|
|
#include "spinlock.h"
|
2006-07-21 15:18:04 +02:00
|
|
|
#include "buf.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "fsvar.h"
|
2006-07-27 23:10:00 +02:00
|
|
|
#include "elf.h"
|
2006-07-29 11:35:02 +02:00
|
|
|
#include "fd.h"
|
2006-08-10 04:07:10 +02:00
|
|
|
#include "fcntl.h"
|
2006-06-15 18:02:20 +02:00
|
|
|
|
2006-09-06 19:50:20 +02:00
|
|
|
// User code makes a system call with INT T_SYSCALL.
|
|
|
|
// System call number in %eax.
|
|
|
|
// Arguments on the stack, from the user call to the C
|
|
|
|
// library system call function. The saved user %esp points
|
|
|
|
// to a saved program counter, and then the first argument.
|
2006-06-15 18:02:20 +02:00
|
|
|
|
2006-09-06 19:50:20 +02:00
|
|
|
// Fetch 32 bits from a user-supplied pointer.
|
|
|
|
// Returns 0 if addr was OK, -1 if illegal.
|
2006-06-26 17:11:19 +02:00
|
|
|
int
|
2006-07-17 03:52:13 +02:00
|
|
|
fetchint(struct proc *p, uint addr, int *ip)
|
2006-06-26 17:11:19 +02:00
|
|
|
{
|
|
|
|
*ip = 0;
|
|
|
|
|
|
|
|
if(addr > p->sz - 4)
|
2006-07-15 19:13:56 +02:00
|
|
|
return -1;
|
2006-07-16 17:38:00 +02:00
|
|
|
*ip = *(int*)(p->mem + addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch byte from a user-supplied pointer.
|
|
|
|
// Returns 0 on success, -1 if pointer is illegal.
|
|
|
|
int
|
2006-09-06 19:27:19 +02:00
|
|
|
fetchbyte(struct proc *p, uint addr, char *c)
|
2006-07-16 17:38:00 +02:00
|
|
|
{
|
|
|
|
if(addr >= p->sz)
|
|
|
|
return -1;
|
|
|
|
*c = *(p->mem + addr);
|
2006-07-15 19:13:56 +02:00
|
|
|
return 0;
|
2006-06-26 17:11:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-07-16 03:15:28 +02:00
|
|
|
fetcharg(int argno, void *ip)
|
2006-06-26 17:11:19 +02:00
|
|
|
{
|
2006-07-17 03:52:13 +02:00
|
|
|
uint esp;
|
2006-06-26 17:11:19 +02:00
|
|
|
|
2006-07-17 03:52:13 +02:00
|
|
|
esp = (uint) curproc[cpu()]->tf->esp;
|
Changes to allow use of native x86 ELF compilers, which on my
Linux 2.4 box using gcc 3.4.6 don't seem to follow the same
conventions as the i386-jos-elf-gcc compilers.
Can run make 'TOOLPREFIX=' or edit the Makefile.
curproc[cpu()] can now be NULL, indicating that no proc is running.
This seemed safer to me than having curproc[0] and curproc[1]
both pointing at proc[0] potentially.
The old implementation of swtch depended on the stack frame layout
used inside swtch being okay to return from on the other stack
(exactly the V6 you are not expected to understand this).
It also could be called in two contexts: at boot time, to schedule
the very first process, and later, on behalf of a process, to sleep
or schedule some other process.
I split this into two functions: scheduler and swtch.
The scheduler is now a separate never-returning function, invoked
by each cpu once set up. The scheduler looks like:
scheduler() {
setjmp(cpu.context);
pick proc to schedule
blah blah blah
longjmp(proc.context)
}
The new swtch is intended to be called only when curproc[cpu()] is not NULL,
that is, only on behalf of a user proc. It does:
swtch() {
if(setjmp(proc.context) == 0)
longjmp(cpu.context)
}
to save the current proc context and then jump over to the scheduler,
running on the cpu stack.
Similarly the system call stubs are now in assembly in usys.S to avoid
needing to know the details of stack frame layout used by the compiler.
Also various changes in the debugging prints.
2006-07-11 03:07:40 +02:00
|
|
|
return fetchint(curproc[cpu()], esp + 4 + 4*argno, ip);
|
2006-06-26 17:11:19 +02:00
|
|
|
}
|
|
|
|
|
2006-09-06 19:50:20 +02:00
|
|
|
// Check that an entire string is valid in user space.
|
|
|
|
// Returns the length, not including null, or -1.
|
2006-07-27 23:10:00 +02:00
|
|
|
int
|
|
|
|
checkstring(uint s)
|
|
|
|
{
|
|
|
|
char c;
|
2006-07-29 00:33:07 +02:00
|
|
|
int len = 0;
|
2006-07-27 23:10:00 +02:00
|
|
|
|
|
|
|
while(1){
|
|
|
|
if(fetchbyte(curproc[cpu()], s, &c) < 0)
|
|
|
|
return -1;
|
|
|
|
if(c == '\0')
|
2006-07-29 00:33:07 +02:00
|
|
|
return len;
|
|
|
|
len++;
|
2006-07-27 23:10:00 +02:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-27 16:35:53 +02:00
|
|
|
int
|
2006-07-18 21:22:37 +02:00
|
|
|
putint(struct proc *p, uint addr, int x)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
|
|
|
if(addr > p->sz - 4)
|
2006-07-15 19:13:56 +02:00
|
|
|
return -1;
|
2006-07-18 21:22:37 +02:00
|
|
|
memmove(p->mem + addr, &x, 4);
|
2006-07-15 19:13:56 +02:00
|
|
|
return 0;
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
2006-09-06 20:19:11 +02:00
|
|
|
extern int sys_chdir(void);
|
|
|
|
extern int sys_close(void);
|
|
|
|
extern int sys_dup(void);
|
|
|
|
extern int sys_exec(void);
|
|
|
|
extern int sys_exit(void);
|
|
|
|
extern int sys_fork(void);
|
|
|
|
extern int sys_fstat(void);
|
|
|
|
extern int sys_getpid(void);
|
|
|
|
extern int sys_kill(void);
|
|
|
|
extern int sys_link(void);
|
|
|
|
extern int sys_mkdir(void);
|
|
|
|
extern int sys_mknod(void);
|
|
|
|
extern int sys_open(void);
|
|
|
|
extern int sys_pipe(void);
|
|
|
|
extern int sys_read(void);
|
|
|
|
extern int sys_sbrk(void);
|
|
|
|
extern int sys_unlink(void);
|
|
|
|
extern int sys_wait(void);
|
|
|
|
extern int sys_write(void);
|
2006-07-27 23:10:00 +02:00
|
|
|
|
2006-06-15 18:02:20 +02:00
|
|
|
void
|
2006-07-15 19:17:00 +02:00
|
|
|
syscall(void)
|
2006-06-15 18:02:20 +02:00
|
|
|
{
|
2006-06-22 22:47:23 +02:00
|
|
|
struct proc *cp = curproc[cpu()];
|
2006-07-17 03:36:39 +02:00
|
|
|
int num = cp->tf->eax;
|
2006-06-26 22:31:52 +02:00
|
|
|
int ret = -1;
|
2006-06-15 18:02:20 +02:00
|
|
|
|
|
|
|
switch(num){
|
|
|
|
case SYS_fork:
|
2006-06-26 22:31:52 +02:00
|
|
|
ret = sys_fork();
|
2006-06-15 18:02:20 +02:00
|
|
|
break;
|
|
|
|
case SYS_exit:
|
2006-06-26 22:31:52 +02:00
|
|
|
ret = sys_exit();
|
2006-06-15 18:02:20 +02:00
|
|
|
break;
|
2006-06-15 21:58:01 +02:00
|
|
|
case SYS_wait:
|
2006-06-26 22:31:52 +02:00
|
|
|
ret = sys_wait();
|
2006-06-15 21:58:01 +02:00
|
|
|
break;
|
2006-06-27 16:35:53 +02:00
|
|
|
case SYS_pipe:
|
|
|
|
ret = sys_pipe();
|
|
|
|
break;
|
|
|
|
case SYS_write:
|
|
|
|
ret = sys_write();
|
|
|
|
break;
|
|
|
|
case SYS_read:
|
|
|
|
ret = sys_read();
|
|
|
|
break;
|
2006-07-01 23:26:01 +02:00
|
|
|
case SYS_close:
|
|
|
|
ret = sys_close();
|
|
|
|
break;
|
2006-07-11 19:39:45 +02:00
|
|
|
case SYS_kill:
|
|
|
|
ret = sys_kill();
|
|
|
|
break;
|
2006-07-27 23:10:00 +02:00
|
|
|
case SYS_exec:
|
|
|
|
ret = sys_exec();
|
|
|
|
break;
|
2006-07-29 11:35:02 +02:00
|
|
|
case SYS_open:
|
|
|
|
ret = sys_open();
|
|
|
|
break;
|
2006-08-08 20:07:37 +02:00
|
|
|
case SYS_mknod:
|
|
|
|
ret = sys_mknod();
|
|
|
|
break;
|
2006-08-10 03:28:57 +02:00
|
|
|
case SYS_unlink:
|
|
|
|
ret = sys_unlink();
|
|
|
|
break;
|
2006-08-12 06:33:50 +02:00
|
|
|
case SYS_fstat:
|
|
|
|
ret = sys_fstat();
|
|
|
|
break;
|
2006-08-13 04:12:44 +02:00
|
|
|
case SYS_link:
|
|
|
|
ret = sys_link();
|
|
|
|
break;
|
2006-08-14 05:00:13 +02:00
|
|
|
case SYS_mkdir:
|
|
|
|
ret = sys_mkdir();
|
|
|
|
break;
|
2006-08-20 01:41:34 +02:00
|
|
|
case SYS_chdir:
|
|
|
|
ret = sys_chdir();
|
|
|
|
break;
|
2006-08-20 05:08:54 +02:00
|
|
|
case SYS_dup:
|
|
|
|
ret = sys_dup();
|
|
|
|
break;
|
2006-08-23 03:09:24 +02:00
|
|
|
case SYS_getpid:
|
|
|
|
ret = sys_getpid();
|
|
|
|
break;
|
|
|
|
case SYS_sbrk:
|
|
|
|
ret = sys_sbrk();
|
|
|
|
break;
|
2006-06-15 18:02:20 +02:00
|
|
|
default:
|
|
|
|
cprintf("unknown sys call %d\n", num);
|
|
|
|
// XXX fault
|
|
|
|
break;
|
|
|
|
}
|
2006-07-17 03:36:39 +02:00
|
|
|
cp->tf->eax = ret;
|
2006-06-15 18:02:20 +02:00
|
|
|
}
|