2006-06-15 18:02:20 +02:00
|
|
|
#include "types.h"
|
|
|
|
#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-06-15 18:02:20 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* User code makes a system call with INT T_SYSCALL.
|
|
|
|
* System call number in %eax.
|
2006-06-26 17:11:19 +02:00
|
|
|
* Arguments on the stack, from the user call to the C
|
|
|
|
* library system call function. The saved user %esp points
|
|
|
|
* to a saved frame pointer, a program counter, and then
|
|
|
|
* the first argument.
|
2006-06-15 18:02:20 +02:00
|
|
|
*
|
|
|
|
* Return value? Error indication? Errno?
|
|
|
|
*/
|
|
|
|
|
2006-06-26 17:11:19 +02:00
|
|
|
/*
|
|
|
|
* fetch 32 bits from a user-supplied pointer.
|
2006-07-15 19:13:56 +02:00
|
|
|
* returns 0 if addr was OK, -1 if illegal.
|
2006-06-26 17:11:19 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
fetchint(struct proc *p, unsigned addr, int *ip)
|
|
|
|
{
|
|
|
|
*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
|
|
|
|
fetchbyte(struct proc *p, unsigned addr, char* c)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-07-16 03:15:28 +02:00
|
|
|
// This arg is void* so that both int* and uint* can be passed.
|
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
|
|
|
{
|
|
|
|
unsigned esp;
|
|
|
|
|
2006-07-16 17:41:47 +02:00
|
|
|
esp = (unsigned) 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-06-27 16:35:53 +02:00
|
|
|
int
|
|
|
|
putint(struct proc *p, unsigned addr, int ip)
|
|
|
|
{
|
|
|
|
if(addr > p->sz - 4)
|
2006-07-15 19:13:56 +02:00
|
|
|
return -1;
|
2006-07-16 03:47:40 +02:00
|
|
|
memmove(p->mem + addr, &ip, 4);
|
2006-07-15 19:13:56 +02:00
|
|
|
return 0;
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-07-15 19:17:00 +02:00
|
|
|
sys_pipe(void)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
|
|
|
struct fd *rfd = 0, *wfd = 0;
|
|
|
|
int f1 = -1, f2 = -1;
|
|
|
|
struct proc *p = curproc[cpu()];
|
|
|
|
unsigned fdp;
|
|
|
|
|
|
|
|
if(pipe_alloc(&rfd, &wfd) < 0)
|
|
|
|
goto oops;
|
|
|
|
if((f1 = fd_ualloc()) < 0)
|
|
|
|
goto oops;
|
|
|
|
p->fds[f1] = rfd;
|
|
|
|
if((f2 = fd_ualloc()) < 0)
|
|
|
|
goto oops;
|
|
|
|
p->fds[f2] = wfd;
|
|
|
|
if(fetcharg(0, &fdp) < 0)
|
|
|
|
goto oops;
|
|
|
|
if(putint(p, fdp, f1) < 0)
|
|
|
|
goto oops;
|
|
|
|
if(putint(p, fdp+4, f2) < 0)
|
|
|
|
goto oops;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
cprintf("sys_pipe failed\n");
|
|
|
|
if(rfd)
|
|
|
|
fd_close(rfd);
|
|
|
|
if(wfd)
|
|
|
|
fd_close(wfd);
|
|
|
|
if(f1 >= 0)
|
|
|
|
p->fds[f1] = 0;
|
|
|
|
if(f2 >= 0)
|
|
|
|
p->fds[f2] = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-07-15 19:17:00 +02:00
|
|
|
sys_write(void)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
2006-07-01 23:26:01 +02:00
|
|
|
int fd, n, ret;
|
2006-06-27 16:35:53 +02:00
|
|
|
unsigned addr;
|
|
|
|
struct proc *p = curproc[cpu()];
|
|
|
|
|
|
|
|
if(fetcharg(0, &fd) < 0 || fetcharg(1, &addr) < 0 || fetcharg(2, &n) < 0)
|
|
|
|
return -1;
|
|
|
|
if(fd < 0 || fd >= NOFILE)
|
|
|
|
return -1;
|
|
|
|
if(p->fds[fd] == 0)
|
|
|
|
return -1;
|
|
|
|
if(addr + n > p->sz)
|
|
|
|
return -1;
|
2006-07-01 23:26:01 +02:00
|
|
|
ret = fd_write(p->fds[fd], p->mem + addr, n);
|
|
|
|
return ret;
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-07-15 19:17:00 +02:00
|
|
|
sys_read(void)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
2006-07-01 23:26:01 +02:00
|
|
|
int fd, n, ret;
|
2006-06-27 16:35:53 +02:00
|
|
|
unsigned addr;
|
|
|
|
struct proc *p = curproc[cpu()];
|
|
|
|
|
|
|
|
if(fetcharg(0, &fd) < 0 || fetcharg(1, &addr) < 0 || fetcharg(2, &n) < 0)
|
|
|
|
return -1;
|
|
|
|
if(fd < 0 || fd >= NOFILE)
|
|
|
|
return -1;
|
|
|
|
if(p->fds[fd] == 0)
|
|
|
|
return -1;
|
|
|
|
if(addr + n > p->sz)
|
|
|
|
return -1;
|
2006-07-01 23:26:01 +02:00
|
|
|
ret = fd_read(p->fds[fd], p->mem + addr, n);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-07-15 19:17:00 +02:00
|
|
|
sys_close(void)
|
2006-07-01 23:26:01 +02:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct proc *p = curproc[cpu()];
|
|
|
|
|
|
|
|
if(fetcharg(0, &fd) < 0)
|
|
|
|
return -1;
|
|
|
|
if(fd < 0 || fd >= NOFILE)
|
|
|
|
return -1;
|
|
|
|
if(p->fds[fd] == 0)
|
|
|
|
return -1;
|
|
|
|
fd_close(p->fds[fd]);
|
|
|
|
p->fds[fd] = 0;
|
|
|
|
return 0;
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
2006-06-26 22:31:52 +02:00
|
|
|
int
|
2006-07-15 19:17:00 +02:00
|
|
|
sys_fork(void)
|
2006-06-15 18:02:20 +02:00
|
|
|
{
|
2006-06-26 22:31:52 +02:00
|
|
|
struct proc *np;
|
|
|
|
|
2006-07-16 03:47:40 +02:00
|
|
|
if((np = copyproc(curproc[cpu()])) == 0)
|
2006-07-15 14:03:57 +02:00
|
|
|
return -1;
|
2006-07-16 03:47:40 +02:00
|
|
|
np->state = RUNNABLE;
|
|
|
|
return np->pid;
|
2006-06-15 18:02:20 +02:00
|
|
|
}
|
|
|
|
|
2006-06-26 22:31:52 +02:00
|
|
|
int
|
2006-07-15 19:17:00 +02:00
|
|
|
sys_exit(void)
|
2006-06-15 18:02:20 +02:00
|
|
|
{
|
2006-07-11 19:39:45 +02:00
|
|
|
proc_exit();
|
2006-07-15 19:24:54 +02:00
|
|
|
return 0; // not reached
|
2006-06-15 18:02:20 +02:00
|
|
|
}
|
|
|
|
|
2006-06-26 22:31:52 +02:00
|
|
|
int
|
2006-07-15 19:17:00 +02:00
|
|
|
sys_wait(void)
|
2006-06-15 21:58:01 +02:00
|
|
|
{
|
2006-07-15 19:24:54 +02:00
|
|
|
return proc_wait();
|
|
|
|
}
|
2006-06-15 21:58:01 +02:00
|
|
|
|
2006-07-15 19:24:54 +02:00
|
|
|
int
|
|
|
|
sys_kill(void)
|
|
|
|
{
|
|
|
|
int pid;
|
2006-06-15 21:58:01 +02:00
|
|
|
|
2006-07-16 17:38:00 +02:00
|
|
|
if(fetcharg(0, &pid) < 0)
|
|
|
|
return -1;
|
2006-07-15 19:24:54 +02:00
|
|
|
return proc_kill(pid);
|
2006-06-15 21:58:01 +02:00
|
|
|
}
|
|
|
|
|
2006-06-26 22:31:52 +02:00
|
|
|
int
|
2006-07-15 19:17:00 +02:00
|
|
|
sys_cons_putc(void)
|
2006-06-26 17:11:19 +02:00
|
|
|
{
|
|
|
|
int c;
|
2006-07-16 17:38:00 +02:00
|
|
|
char buf[2];
|
|
|
|
|
|
|
|
if(fetcharg(0, &c) < 0)
|
|
|
|
return -1;
|
|
|
|
buf[0] = c;
|
|
|
|
buf[1] = 0;
|
|
|
|
cprintf("%s", buf);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-06-26 17:11:19 +02:00
|
|
|
|
2006-07-16 17:38:00 +02:00
|
|
|
int
|
|
|
|
sys_cons_puts(void)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
int i;
|
|
|
|
unsigned addr;
|
|
|
|
struct proc *cp = curproc[cpu()];
|
|
|
|
|
|
|
|
if(fetcharg(0, &addr) < 0)
|
|
|
|
return -1;
|
|
|
|
for(i=0; i<sizeof buf-1 && fetchbyte(cp, addr+i, &buf[i]) >= 0; i++)
|
|
|
|
if(buf[i] == 0)
|
|
|
|
break;
|
|
|
|
buf[i] = 0;
|
|
|
|
cprintf("%s", buf);
|
2006-06-26 22:31:52 +02:00
|
|
|
return 0;
|
2006-06-26 17:11:19 +02:00
|
|
|
}
|
|
|
|
|
2006-07-06 23:47:22 +02:00
|
|
|
int
|
|
|
|
sys_block(void)
|
|
|
|
{
|
2006-07-10 15:08:37 +02:00
|
|
|
char buf[512];
|
|
|
|
int i, j;
|
2006-07-10 21:06:48 +02:00
|
|
|
void *c;
|
2006-07-06 23:47:22 +02:00
|
|
|
|
|
|
|
cprintf("%d: call sys_block\n", cpu());
|
2006-07-10 15:08:37 +02:00
|
|
|
for (i = 0; i < 100; i++) {
|
2006-07-10 21:06:48 +02:00
|
|
|
if ((c = ide_start_read(i, buf, 1)) == 0) {
|
2006-07-10 15:08:37 +02:00
|
|
|
panic("couldn't start read\n");
|
|
|
|
}
|
|
|
|
cprintf("call sleep\n");
|
2006-07-15 14:03:57 +02:00
|
|
|
sleep (c, 0);
|
2006-07-10 21:06:48 +02:00
|
|
|
if (ide_finish_read(c)) {
|
2006-07-10 15:08:37 +02:00
|
|
|
panic("couldn't do read\n");
|
|
|
|
}
|
|
|
|
cprintf("sector %d: ", i);
|
|
|
|
for (j = 0; j < 2; j++)
|
|
|
|
cprintf("%x ", buf[j] & 0xff);
|
|
|
|
cprintf("\n");
|
|
|
|
}
|
2006-07-06 23:47:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-15 14:03:57 +02:00
|
|
|
int
|
2006-07-15 19:17:00 +02:00
|
|
|
sys_panic(void)
|
2006-07-15 14:03:57 +02:00
|
|
|
{
|
|
|
|
struct proc *p = curproc[cpu()];
|
|
|
|
unsigned int addr;
|
|
|
|
|
2006-07-16 17:38:00 +02:00
|
|
|
if(fetcharg(0, &addr) < 0)
|
|
|
|
return -1;
|
2006-07-15 14:03:57 +02:00
|
|
|
panic(p->mem + addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-16 17:41:47 +02:00
|
|
|
int num = cp->tf->regs.eax;
|
2006-06-26 22:31:52 +02:00
|
|
|
int ret = -1;
|
2006-06-15 18:02:20 +02:00
|
|
|
|
2006-06-27 16:35:53 +02:00
|
|
|
//cprintf("%x sys %d\n", cp, num);
|
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-26 17:11:19 +02:00
|
|
|
case SYS_cons_putc:
|
2006-06-26 22:31:52 +02:00
|
|
|
ret = sys_cons_putc();
|
2006-06-26 17:11:19 +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-06 23:47:22 +02:00
|
|
|
case SYS_block:
|
|
|
|
ret = sys_block();
|
|
|
|
break;
|
2006-07-11 19:39:45 +02:00
|
|
|
case SYS_kill:
|
|
|
|
ret = sys_kill();
|
|
|
|
break;
|
2006-07-15 14:03:57 +02:00
|
|
|
case SYS_panic:
|
|
|
|
ret = sys_panic();
|
|
|
|
break;
|
2006-07-16 17:41:47 +02:00
|
|
|
case SYS_cons_puts:
|
|
|
|
ret = sys_cons_puts();
|
|
|
|
break;
|
2006-06-15 18:02:20 +02:00
|
|
|
default:
|
|
|
|
cprintf("unknown sys call %d\n", num);
|
|
|
|
// XXX fault
|
|
|
|
break;
|
|
|
|
}
|
2006-07-16 17:41:47 +02:00
|
|
|
cp->tf->regs.eax = ret;
|
2006-06-15 18:02:20 +02:00
|
|
|
}
|