10 // User code makes a system call with INT T_SYSCALL.
11 // System call number in %eax.
12 // Arguments on the stack, from the user call to the C
13 // library system call function. The saved user %esp points
14 // to a saved program counter, and then the first argument.
16 // Fetch the int at addr from the current process.
18 fetchint(uint addr
, int *ip
)
20 if(addr
>= proc
->sz
|| addr
+4 > proc
->sz
)
26 // Fetch the nul-terminated string at addr from the current process.
27 // Doesn't actually copy the string - just sets *pp to point at it.
28 // Returns length of string, not including nul.
30 fetchstr(uint addr
, char **pp
)
38 for(s
= *pp
; s
< ep
; s
++)
44 // Fetch the nth 32-bit system call argument.
46 argint(int n
, int *ip
)
48 return fetchint(proc
->tf
->esp
+ 4 + 4*n
, ip
);
51 // Fetch the nth word-sized system call argument as a pointer
52 // to a block of memory of size n bytes. Check that the pointer
53 // lies within the process address space.
55 argptr(int n
, char **pp
, int size
)
61 if((uint
)i
>= proc
->sz
|| (uint
)i
+size
> proc
->sz
)
67 // Fetch the nth word-sized system call argument as a string pointer.
68 // Check that the pointer is valid and the string is nul-terminated.
69 // (There is no shared writable memory, so the string can't change
70 // between this check and being used by the kernel.)
72 argstr(int n
, char **pp
)
75 if(argint(n
, &addr
) < 0)
77 return fetchstr(addr
, pp
);
80 extern int sys_chdir(void);
81 extern int sys_close(void);
82 extern int sys_dup(void);
83 extern int sys_exec(void);
84 extern int sys_exit(void);
85 extern int sys_fork(void);
86 extern int sys_fstat(void);
87 extern int sys_getpid(void);
88 extern int sys_kill(void);
89 extern int sys_link(void);
90 extern int sys_mkdir(void);
91 extern int sys_mknod(void);
92 extern int sys_open(void);
93 extern int sys_pipe(void);
94 extern int sys_read(void);
95 extern int sys_sbrk(void);
96 extern int sys_sleep(void);
97 extern int sys_unlink(void);
98 extern int sys_wait(void);
99 extern int sys_write(void);
100 extern int sys_uptime(void);
102 static int (*syscalls
[])(void) = {
110 [SYS_fstat
] sys_fstat
,
111 [SYS_chdir
] sys_chdir
,
113 [SYS_getpid
] sys_getpid
,
115 [SYS_sleep
] sys_sleep
,
116 [SYS_uptime
] sys_uptime
,
118 [SYS_write
] sys_write
,
119 [SYS_mknod
] sys_mknod
,
120 [SYS_unlink
] sys_unlink
,
122 [SYS_mkdir
] sys_mkdir
,
123 [SYS_close
] sys_close
,
132 if(num
>= 0 && num
< SYS_open
&& syscalls
[num
]) {
133 proc
->tf
->eax
= syscalls
[num
]();
134 } else if (num
>= SYS_open
&& num
< NELEM(syscalls
) && syscalls
[num
]) {
135 proc
->tf
->eax
= syscalls
[num
]();
137 cprintf("%d %s: unknown sys call %d\n",
138 proc
->pid
, proc
->name
, num
);