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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2006-07-18 21:22:37 +02:00
|
|
|
* to a saved program counter, and then the first argument.
|
2006-06-15 18:02:20 +02:00
|
|
|
*/
|
|
|
|
|
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
|
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-07-17 03:52:13 +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-07-29 00:33:07 +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
|
|
|
}
|
|
|
|
|
|
|
|
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()];
|
2006-07-17 03:52:13 +02:00
|
|
|
uint fdp;
|
2006-06-27 16:35:53 +02:00
|
|
|
|
|
|
|
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:
|
|
|
|
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-07-17 03:52:13 +02:00
|
|
|
uint addr;
|
2006-06-27 16:35:53 +02:00
|
|
|
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-08-23 03:09:24 +02:00
|
|
|
|
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-07-17 03:52:13 +02:00
|
|
|
uint addr;
|
2006-06-27 16:35:53 +02:00
|
|
|
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-07-29 11:35:02 +02:00
|
|
|
int
|
|
|
|
sys_open(void)
|
|
|
|
{
|
|
|
|
struct proc *cp = curproc[cpu()];
|
2006-08-25 03:11:30 +02:00
|
|
|
struct inode *ip, *dp;
|
2006-07-29 11:35:02 +02:00
|
|
|
uint arg0, arg1;
|
|
|
|
int ufd;
|
|
|
|
struct fd *fd;
|
2006-08-09 21:25:20 +02:00
|
|
|
int l;
|
2006-08-25 03:11:30 +02:00
|
|
|
char *last;
|
2006-07-29 11:35:02 +02:00
|
|
|
|
|
|
|
if(fetcharg(0, &arg0) < 0 || fetcharg(1, &arg1) < 0)
|
|
|
|
return -1;
|
2006-08-09 21:25:20 +02:00
|
|
|
if((l = checkstring(arg0)) < 0)
|
2006-07-29 11:35:02 +02:00
|
|
|
return -1;
|
2006-08-25 03:11:30 +02:00
|
|
|
|
|
|
|
if(arg1 & O_CREATE){
|
|
|
|
dp = namei(cp->mem + arg0, NAMEI_CREATE, 0, &last, &ip);
|
|
|
|
if(dp){
|
|
|
|
ip = mknod1(dp, last, T_FILE, 0, 0);
|
|
|
|
iput(dp);
|
|
|
|
if(ip == 0)
|
|
|
|
return -1;
|
|
|
|
} else if(ip == 0){
|
|
|
|
return -1;
|
2006-08-26 18:31:05 +02:00
|
|
|
} else if(ip->type == T_DIR){
|
|
|
|
iput(ip);
|
|
|
|
return -1;
|
2006-08-25 03:11:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ip = namei(cp->mem + arg0, NAMEI_LOOKUP, 0, 0, 0);
|
|
|
|
if(ip == 0)
|
|
|
|
return -1;
|
2006-08-09 21:25:20 +02:00
|
|
|
}
|
2006-08-26 18:31:05 +02:00
|
|
|
if(ip->type == T_DIR && ((arg1 & O_RDWR) || (arg1 & O_WRONLY))){
|
|
|
|
iput(ip);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-08-25 03:11:30 +02:00
|
|
|
|
2006-07-29 11:35:02 +02:00
|
|
|
if((fd = fd_alloc()) == 0){
|
|
|
|
iput(ip);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if((ufd = fd_ualloc()) < 0){
|
|
|
|
iput(ip);
|
|
|
|
fd_close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iunlock(ip);
|
|
|
|
fd->type = FD_FILE;
|
2006-08-09 21:25:20 +02:00
|
|
|
if (arg1 & O_RDWR) {
|
2006-08-09 18:04:04 +02:00
|
|
|
fd->readable = 1;
|
|
|
|
fd->writeable = 1;
|
2006-08-09 21:25:20 +02:00
|
|
|
} else if (arg1 & O_WRONLY) {
|
|
|
|
fd->readable = 0;
|
|
|
|
fd->writeable = 1;
|
2006-08-09 18:04:04 +02:00
|
|
|
} else {
|
|
|
|
fd->readable = 1;
|
|
|
|
fd->writeable = 0;
|
|
|
|
}
|
2006-07-29 11:35:02 +02:00
|
|
|
fd->ip = ip;
|
2006-08-08 21:58:06 +02:00
|
|
|
fd->off = 0;
|
2006-07-29 11:35:02 +02:00
|
|
|
cp->fds[ufd] = fd;
|
|
|
|
|
|
|
|
return ufd;
|
|
|
|
}
|
|
|
|
|
2006-08-08 20:07:37 +02:00
|
|
|
int
|
|
|
|
sys_mknod(void)
|
|
|
|
{
|
|
|
|
struct proc *cp = curproc[cpu()];
|
2006-08-12 03:25:45 +02:00
|
|
|
struct inode *nip;
|
2006-08-08 20:07:37 +02:00
|
|
|
uint arg0, arg1, arg2, arg3;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
if(fetcharg(0, &arg0) < 0 || fetcharg(1, &arg1) < 0 ||
|
|
|
|
fetcharg(2, &arg2) < 0 || fetcharg(3, &arg3) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if((l = checkstring(arg0)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(l >= DIRSIZ)
|
|
|
|
return -1;
|
|
|
|
|
2006-08-12 03:25:45 +02:00
|
|
|
nip = mknod (cp->mem + arg0, (short) arg1, (short) arg2, (short) arg3);
|
2006-08-13 17:51:58 +02:00
|
|
|
if(nip)
|
|
|
|
iput(nip);
|
2006-08-11 20:18:38 +02:00
|
|
|
return (nip == 0) ? -1 : 0;
|
2006-08-08 20:07:37 +02:00
|
|
|
}
|
|
|
|
|
2006-08-14 05:00:13 +02:00
|
|
|
int
|
|
|
|
sys_mkdir(void)
|
|
|
|
{
|
|
|
|
struct proc *cp = curproc[cpu()];
|
|
|
|
struct inode *nip;
|
2006-08-26 18:31:05 +02:00
|
|
|
struct inode *dp;
|
2006-08-14 05:00:13 +02:00
|
|
|
uint arg0;
|
|
|
|
int l;
|
2006-08-14 16:13:52 +02:00
|
|
|
struct dirent de;
|
2006-08-26 18:31:05 +02:00
|
|
|
char *last;
|
2006-08-14 05:00:13 +02:00
|
|
|
|
|
|
|
if(fetcharg(0, &arg0) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if((l = checkstring(arg0)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2006-08-26 18:31:05 +02:00
|
|
|
dp = namei(cp->mem + arg0, NAMEI_CREATE, 0, &last, 0);
|
|
|
|
if(dp == 0)
|
2006-08-14 05:00:13 +02:00
|
|
|
return -1;
|
|
|
|
|
2006-08-26 18:31:05 +02:00
|
|
|
nip = mknod1(dp, last, T_DIR, 0, 0);
|
|
|
|
if(nip == 0){
|
|
|
|
iput(dp);
|
2006-08-26 17:06:59 +02:00
|
|
|
return -1;
|
2006-08-26 18:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dp->nlink += 1;
|
|
|
|
iupdate(dp);
|
2006-08-20 01:41:34 +02:00
|
|
|
|
|
|
|
memset (de.name, '\0', DIRSIZ);
|
2006-08-14 16:13:52 +02:00
|
|
|
de.name[0] = '.';
|
|
|
|
de.inum = nip->inum;
|
|
|
|
writei (nip, (char *) &de, 0, sizeof(de));
|
|
|
|
|
2006-08-26 18:31:05 +02:00
|
|
|
de.inum = dp->inum;
|
2006-08-14 16:13:52 +02:00
|
|
|
de.name[1] = '.';
|
|
|
|
writei (nip, (char *) &de, sizeof(de), sizeof(de));
|
2006-08-14 05:00:13 +02:00
|
|
|
|
2006-08-26 18:31:05 +02:00
|
|
|
iput(dp);
|
2006-08-14 05:00:13 +02:00
|
|
|
iput(nip);
|
2006-08-26 18:31:05 +02:00
|
|
|
|
|
|
|
return 0;
|
2006-08-14 05:00:13 +02:00
|
|
|
}
|
|
|
|
|
2006-08-20 01:41:34 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
sys_chdir(void)
|
|
|
|
{
|
|
|
|
struct proc *cp = curproc[cpu()];
|
|
|
|
struct inode *ip;
|
|
|
|
uint arg0;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
if(fetcharg(0, &arg0) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if((l = checkstring(arg0)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2006-08-25 03:11:30 +02:00
|
|
|
if ((ip = namei(cp->mem + arg0, NAMEI_LOOKUP, 0, 0, 0)) == 0)
|
2006-08-20 01:41:34 +02:00
|
|
|
return -1;
|
2006-08-26 17:06:59 +02:00
|
|
|
|
2006-08-20 01:41:34 +02:00
|
|
|
if (ip == cp->cwd) {
|
|
|
|
iput (ip);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ip->type != T_DIR) {
|
|
|
|
iput(ip);
|
2006-08-26 17:06:59 +02:00
|
|
|
return -1;
|
2006-08-20 01:41:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
idecref(cp->cwd);
|
|
|
|
cp->cwd = ip;
|
|
|
|
iunlock(cp->cwd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-10 03:28:57 +02:00
|
|
|
int
|
|
|
|
sys_unlink(void)
|
|
|
|
{
|
|
|
|
struct proc *cp = curproc[cpu()];
|
|
|
|
uint arg0;
|
2006-08-11 20:18:38 +02:00
|
|
|
int r;
|
|
|
|
|
2006-08-10 03:28:57 +02:00
|
|
|
if(fetcharg(0, &arg0) < 0)
|
|
|
|
return -1;
|
|
|
|
if(checkstring(arg0) < 0)
|
|
|
|
return -1;
|
2006-08-11 20:18:38 +02:00
|
|
|
r = unlink(cp->mem + arg0);
|
|
|
|
return r;
|
2006-08-10 03:28:57 +02:00
|
|
|
}
|
|
|
|
|
2006-08-12 06:33:50 +02:00
|
|
|
int
|
|
|
|
sys_fstat(void)
|
|
|
|
{
|
|
|
|
struct proc *cp = curproc[cpu()];
|
|
|
|
uint fd, addr;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if(fetcharg(0, &fd) < 0)
|
|
|
|
return -1;
|
|
|
|
if(fetcharg(1, &addr) < 0)
|
|
|
|
return -1;
|
|
|
|
if(fd < 0 || fd >= NOFILE)
|
|
|
|
return -1;
|
|
|
|
if(cp->fds[fd] == 0)
|
|
|
|
return -1;
|
|
|
|
if(addr + sizeof(struct stat) > cp->sz)
|
|
|
|
return -1;
|
|
|
|
r = fd_stat (cp->fds[fd], (struct stat *)(cp->mem + addr));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-08-20 05:08:54 +02:00
|
|
|
int
|
|
|
|
sys_dup(void)
|
|
|
|
{
|
|
|
|
struct proc *cp = curproc[cpu()];
|
2006-08-20 05:33:01 +02:00
|
|
|
uint fd, ufd1;
|
2006-08-20 05:08:54 +02:00
|
|
|
|
|
|
|
if(fetcharg(0, &fd) < 0)
|
|
|
|
return -1;
|
|
|
|
if(fd < 0 || fd >= NOFILE)
|
|
|
|
return -1;
|
2006-08-20 05:33:01 +02:00
|
|
|
if(cp->fds[fd] == 0)
|
2006-08-20 05:08:54 +02:00
|
|
|
return -1;
|
2006-08-30 20:55:06 +02:00
|
|
|
if ((ufd1 = fd_ualloc()) < 0)
|
2006-08-20 05:33:01 +02:00
|
|
|
return -1;
|
2006-08-24 04:44:41 +02:00
|
|
|
cp->fds[ufd1] = cp->fds[fd];
|
|
|
|
fd_incref(cp->fds[ufd1]);
|
2006-08-20 05:33:01 +02:00
|
|
|
return ufd1;
|
2006-08-20 05:08:54 +02:00
|
|
|
}
|
|
|
|
|
2006-08-13 04:12:44 +02:00
|
|
|
int
|
|
|
|
sys_link(void)
|
|
|
|
{
|
|
|
|
struct proc *cp = curproc[cpu()];
|
|
|
|
uint name1, name2;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if(fetcharg(0, &name1) < 0 || checkstring(name1) < 0)
|
|
|
|
return -1;
|
|
|
|
if(fetcharg(1, &name2) < 0 || checkstring(name2) < 0)
|
|
|
|
return -1;
|
|
|
|
r = link(cp->mem + name1, cp->mem + name2);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-08-23 03:09:24 +02:00
|
|
|
int
|
|
|
|
sys_getpid(void)
|
|
|
|
{
|
|
|
|
struct proc *cp = curproc[cpu()];
|
|
|
|
return cp->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_sbrk(void)
|
|
|
|
{
|
2006-08-29 21:06:37 +02:00
|
|
|
uint addr;
|
2006-08-24 04:44:41 +02:00
|
|
|
int n;
|
2006-08-23 03:09:24 +02:00
|
|
|
struct proc *cp = curproc[cpu()];
|
|
|
|
|
|
|
|
if(fetcharg(0, &n) < 0)
|
|
|
|
return -1;
|
2006-08-29 19:01:40 +02:00
|
|
|
if((addr = growproc(n)) == 0xffffffff)
|
2006-08-29 16:45:45 +02:00
|
|
|
return -1;
|
2006-08-23 03:09:24 +02:00
|
|
|
setupsegs(cp);
|
2006-08-29 19:01:40 +02:00
|
|
|
return addr;
|
2006-08-23 03:09:24 +02:00
|
|
|
}
|
|
|
|
|
2006-07-27 23:10:00 +02:00
|
|
|
int
|
|
|
|
sys_exec(void)
|
|
|
|
{
|
|
|
|
struct proc *cp = curproc[cpu()];
|
2006-07-29 00:33:07 +02:00
|
|
|
uint arg0, arg1, sz=0, ap, sp, p1, p2;
|
|
|
|
int i, nargs, argbytes, len;
|
2006-07-27 23:10:00 +02:00
|
|
|
struct inode *ip;
|
|
|
|
struct elfhdr elf;
|
|
|
|
struct proghdr ph;
|
2006-07-29 00:33:07 +02:00
|
|
|
char *mem = 0;
|
2006-07-27 23:10:00 +02:00
|
|
|
|
|
|
|
if(fetcharg(0, &arg0) < 0)
|
|
|
|
return -1;
|
2006-07-29 00:33:07 +02:00
|
|
|
if(fetcharg(1, &arg1) < 0)
|
|
|
|
return -1;
|
2006-07-27 23:10:00 +02:00
|
|
|
if(checkstring(arg0) < 0)
|
|
|
|
return -1;
|
2006-08-25 03:11:30 +02:00
|
|
|
ip = namei(cp->mem + arg0, NAMEI_LOOKUP, 0, 0, 0);
|
2006-07-27 23:10:00 +02:00
|
|
|
if(ip == 0)
|
|
|
|
return -1;
|
|
|
|
|
2006-08-11 15:55:18 +02:00
|
|
|
if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
|
2006-07-27 23:10:00 +02:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
if(elf.magic != ELF_MAGIC)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
sz = 0;
|
|
|
|
for(i = 0; i < elf.phnum; i++){
|
2006-08-11 15:55:18 +02:00
|
|
|
if(readi(ip, (char*)&ph, elf.phoff + i * sizeof(ph),
|
|
|
|
sizeof(ph)) != sizeof(ph))
|
2006-07-27 23:10:00 +02:00
|
|
|
goto bad;
|
|
|
|
if(ph.type != ELF_PROG_LOAD)
|
|
|
|
continue;
|
|
|
|
if(ph.memsz < ph.filesz)
|
|
|
|
goto bad;
|
|
|
|
sz += ph.memsz;
|
|
|
|
}
|
|
|
|
|
|
|
|
sz += 4096 - (sz % 4096);
|
|
|
|
sz += 4096;
|
|
|
|
|
2006-07-29 00:33:07 +02:00
|
|
|
mem = kalloc(sz);
|
|
|
|
if(mem == 0)
|
|
|
|
goto bad;
|
|
|
|
memset(mem, 0, sz);
|
|
|
|
|
|
|
|
// arg1 is a pointer to an array of pointers to string.
|
|
|
|
nargs = 0;
|
|
|
|
argbytes = 0;
|
|
|
|
for(i = 0; ; i++){
|
|
|
|
if(fetchint(cp, arg1 + 4*i, &ap) < 0)
|
|
|
|
goto bad;
|
|
|
|
if(ap == 0)
|
|
|
|
break;
|
|
|
|
len = checkstring(ap);
|
|
|
|
if(len < 0)
|
|
|
|
goto bad;
|
|
|
|
nargs++;
|
|
|
|
argbytes += len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// argn\0
|
|
|
|
// ...
|
|
|
|
// arg0\0
|
|
|
|
// 0
|
|
|
|
// ptr to argn
|
|
|
|
// ...
|
|
|
|
// 12: ptr to arg0
|
|
|
|
// 8: argv (points to ptr to arg0)
|
|
|
|
// 4: argc
|
|
|
|
// 0: fake return pc
|
|
|
|
sp = sz - argbytes - (nargs+1)*4 - 4 - 4 - 4;
|
|
|
|
*(uint*)(mem + sp) = 0xffffffff;
|
|
|
|
*(uint*)(mem + sp + 4) = nargs;
|
|
|
|
*(uint*)(mem + sp + 8) = (uint)(sp + 12);
|
|
|
|
|
|
|
|
p1 = sp + 12;
|
|
|
|
p2 = sp + 12 + (nargs + 1) * 4;
|
|
|
|
for(i = 0; i < nargs; i++){
|
|
|
|
fetchint(cp, arg1 + 4*i, &ap);
|
|
|
|
len = checkstring(ap);
|
|
|
|
memmove(mem + p2, cp->mem + ap, len + 1);
|
|
|
|
*(uint*)(mem + p1) = p2;
|
|
|
|
p1 += 4;
|
|
|
|
p2 += len + 1;
|
|
|
|
}
|
|
|
|
*(uint*)(mem + p1) = 0;
|
|
|
|
|
2006-07-27 23:10:00 +02:00
|
|
|
// commit to the new image.
|
|
|
|
kfree(cp->mem, cp->sz);
|
|
|
|
cp->sz = sz;
|
2006-07-29 00:33:07 +02:00
|
|
|
cp->mem = mem;
|
|
|
|
mem = 0;
|
2006-07-27 23:10:00 +02:00
|
|
|
|
|
|
|
for(i = 0; i < elf.phnum; i++){
|
2006-08-11 15:55:18 +02:00
|
|
|
if(readi(ip, (char*)&ph, elf.phoff + i * sizeof(ph),
|
|
|
|
sizeof(ph)) != sizeof(ph))
|
2006-07-29 00:33:07 +02:00
|
|
|
goto bad2;
|
2006-07-27 23:10:00 +02:00
|
|
|
if(ph.type != ELF_PROG_LOAD)
|
|
|
|
continue;
|
|
|
|
if(ph.va + ph.memsz > sz)
|
|
|
|
goto bad2;
|
|
|
|
if(readi(ip, cp->mem + ph.va, ph.offset, ph.filesz) != ph.filesz)
|
|
|
|
goto bad2;
|
|
|
|
memset(cp->mem + ph.va + ph.filesz, 0, ph.memsz - ph.filesz);
|
|
|
|
}
|
|
|
|
|
|
|
|
iput(ip);
|
|
|
|
|
|
|
|
cp->tf->eip = elf.entry;
|
2006-07-29 00:33:07 +02:00
|
|
|
cp->tf->esp = sp;
|
2006-07-27 23:10:00 +02:00
|
|
|
setupsegs(cp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bad:
|
2006-07-29 00:33:07 +02:00
|
|
|
if(mem)
|
|
|
|
kfree(mem, sz);
|
2006-07-27 23:10:00 +02:00
|
|
|
iput(ip);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
bad2:
|
|
|
|
iput(ip);
|
|
|
|
proc_exit();
|
|
|
|
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-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
|
|
|
}
|