Skip to content

Commit 79c0e5f

Browse files
committed
Implement system calls
1 parent 4a878ca commit 79c0e5f

File tree

4 files changed

+101
-40
lines changed

4 files changed

+101
-40
lines changed

kernel/mm/vm_syscall.c

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,20 @@ u64 sys_handle_brk(u64 addr)
323323
u64 retval;
324324
int ret;
325325

326+
int pmo_cap;
327+
326328
vmspace = obj_get(current_process, VMSPACE_OBJ_ID, TYPE_VMSPACE);
327329

328330
/*
329-
* Lab3: Your code here
330331
* The sys_handle_brk syscall modifies the top address of heap to addr.
331332
*
332-
* If addr is 0, this function should initialize the heap, implemeted by:
333+
* If addr is 0, this function should initialize the heap, implemented by:
333334
* 1. Create a new pmo with size 0 and type PMO_ANONYM.
334335
* 2. Initialize vmspace->heap_vmr using function init_heap_vmr(), which generates
335-
* the mapping between user heap's virtual address (already stored in
336+
* the mapping between user heap's virtual address (already stored in
336337
* vmspace->user_current_heap) and the pmo you just created.
337338
*
338-
* HINT: For more details about how to create and initiailze a pmo, check function
339+
* HINT: For more details about how to create and initialize a pmo, check function
339340
* 'load_binary' for reference.
340341
*
341342
* If addr is larger than heap, the size of vmspace->heap_vmr and the size of its
@@ -348,10 +349,51 @@ u64 sys_handle_brk(u64 addr)
348349
*
349350
*/
350351

352+
if (addr == 0) {
353+
pmo = obj_alloc(TYPE_PMO, sizeof(*pmo));
354+
pmo_cap = -1;
355+
if (!pmo) {
356+
ret = -ENOMEM;
357+
goto out_free_cap;
358+
}
359+
pmo_init(pmo, PMO_ANONYM, 0, 0);
360+
pmo_cap = cap_alloc(current_process, pmo, 0);
361+
if (pmo_cap < 0) {
362+
ret = pmo_cap;
363+
goto out_free_obj;
364+
}
365+
vmr = init_heap_vmr(vmspace, vmspace->user_current_heap, pmo);
366+
if (!vmr) {
367+
ret = -ENOMEM;
368+
goto out_fail;
369+
}
370+
vmspace->heap_vmr = vmr;
371+
retval = vmspace->user_current_heap;
372+
} else {
373+
vmr = vmspace->heap_vmr;
374+
pmo = vmspace->heap_vmr->pmo;
375+
len = addr - vmr->start - vmr->size;
376+
if (len > 0) {
377+
vmr->size += len;
378+
pmo->size += len;
379+
retval = addr;
380+
} else {
381+
ret = -EINVAL;
382+
goto out_fail;
383+
}
384+
}
385+
351386
/*
352387
* return origin heap addr on failure;
353388
* return new heap addr on success.
354389
*/
355390
obj_put(vmspace);
356391
return retval;
392+
out_free_obj:
393+
obj_free(pmo);
394+
out_free_cap:
395+
if (pmo_cap != 0)
396+
cap_free(current_process, pmo_cap);
397+
out_fail:
398+
return ret;
357399
}

kernel/syscall/syscall.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@ void sys_debug(long arg)
2626
void sys_putc(char ch)
2727
{
2828
/*
29-
* Lab3: Your code here
3029
* Send ch to the screen in anyway as your like
3130
*/
31+
uart_send(ch);
3232
}
3333

3434
/*
35-
* Lab3: Your code here
3635
* Update the syscall table as you like to redirect syscalls
3736
* to functions accordingly
3837
*/
3938
const void *syscall_table[NR_SYSCALL] = {
4039
[0 ... NR_SYSCALL - 1] = sys_debug,
40+
[SYS_putc] = sys_putc,
41+
[SYS_exit] = sys_exit,
42+
[SYS_create_pmo] = sys_create_pmo,
43+
[SYS_map_pmo] = sys_map_pmo,
44+
[SYS_handle_brk] = sys_handle_brk,
4145
/* lab3 syscalls finished */
4246
};

user/lib/libmain.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ void _start_c(long *p)
1010

1111
int ret = main(argc, argv, envp);
1212
/*
13-
* Lab3: Your code here
1413
* Complete the main function
1514
*/
16-
return;
15+
usys_exit(ret);
1716
}

user/lib/syscall.c

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,95 @@
22
#include <lib/syscall.h>
33
#include <lib/type.h>
44

5-
u64 syscall(u64 sys_no, u64 arg0, u64 arg1, u64 arg2, u64 arg3, u64 arg4,
6-
u64 arg5, u64 arg6, u64 arg7, u64 arg8)
5+
u64 syscall(u64 sys_no, u64 arg0, u64 arg1, u64 arg2, u64 arg3,
6+
u64 arg4, u64 arg5, u64 arg6, u64 arg7)
77
{
88

99
u64 ret = 0;
1010
/*
11-
* Lab3: Your code here
1211
* Use inline assembly to store arguments into x0 to x7, store syscall number to x8,
1312
* And finally use svc to execute the system call. After syscall returned, don't forget
1413
* to move return value from x0 to the ret variable of this function
1514
*/
15+
asm volatile (
16+
"mov x8, %1\n\t"
17+
"mov x0, %2\n\t"
18+
"mov x1, %3\n\t"
19+
"mov x2, %4\n\t"
20+
"mov x3, %5\n\t"
21+
"mov x4, %6\n\t"
22+
"mov x5, %7\n\t"
23+
"mov x6, %8\n\t"
24+
"mov x7, %9\n\t"
25+
"svc #0\n\t"
26+
"mov %0, x0\n\t"
27+
: "=r"(ret)
28+
: "r"(sys_no), "r"(arg0), "r"(arg1), "r"(arg2), "r"(arg3),
29+
"r"(arg4), "r"(arg5), "r"(arg6), "r"(arg7)
30+
: "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8"
31+
);
1632
return ret;
1733
}
1834

1935
/*
20-
* Lab3: your code here:
2136
* Finish the following system calls using helper function syscall
2237
*/
2338
void usys_putc(char ch)
2439
{
40+
syscall(SYS_putc, (u64) ch, 0, 0, 0, 0, 0, 0, 0);
2541
}
2642

2743
void usys_exit(int ret)
2844
{
45+
syscall(SYS_exit, (u64) ret, 0, 0, 0, 0, 0, 0, 0);
2946
}
3047

3148
int usys_create_pmo(u64 size, u64 type)
3249
{
33-
return 0;
50+
return syscall(SYS_create_pmo, size, type, 0, 0, 0, 0, 0, 0);
3451
}
3552

3653
int usys_map_pmo(u64 process_cap, u64 pmo_cap, u64 addr, u64 rights)
3754
{
38-
return 0;
55+
return syscall(SYS_map_pmo, process_cap, pmo_cap, addr, rights, 0, 0, 0, 0);
3956
}
4057

4158
u64 usys_handle_brk(u64 addr)
4259
{
43-
return 0;
60+
return syscall(SYS_handle_brk, addr, 0, 0, 0, 0, 0, 0, 0);
4461
}
4562

4663
/* Here finishes all syscalls need by lab3 */
4764

4865
u32 usys_getc(void)
4966
{
50-
return (u32) syscall(SYS_getc, 0, 0, 0, 0, 0, 0, 0, 0, 0);
67+
return (u32) syscall(SYS_getc, 0, 0, 0, 0, 0, 0, 0, 0);
5168
}
5269

5370
u64 usys_yield(void)
5471
{
55-
return syscall(SYS_yield, 0, 0, 0, 0, 0, 0, 0, 0, 0);
72+
return syscall(SYS_yield, 0, 0, 0, 0, 0, 0, 0, 0);
5673
}
5774

5875
int usys_create_device_pmo(u64 paddr, u64 size)
5976
{
60-
return syscall(SYS_create_device_pmo, paddr, size, 0, 0, 0, 0, 0, 0, 0);
77+
return syscall(SYS_create_device_pmo, paddr, size, 0, 0, 0, 0, 0, 0);
6178
}
6279

6380
int usys_unmap_pmo(u64 process_cap, u64 pmo_cap, u64 addr)
6481
{
6582
return syscall(SYS_unmap_pmo, process_cap, pmo_cap, addr,
66-
0, 0, 0, 0, 0, 0);
83+
0, 0, 0, 0, 0);
6784
}
6885

6986
int usys_set_affinity(u64 thread_cap, s32 aff)
7087
{
71-
return syscall(SYS_set_affinity, thread_cap, (u64) aff, 0, 0, 0, 0, 0,
72-
0, 0);
88+
return syscall(SYS_set_affinity, thread_cap, (u64) aff, 0, 0, 0, 0, 0, 0);
7389
}
7490

7591
s32 usys_get_affinity(u64 thread_cap)
7692
{
77-
return syscall(SYS_get_affinity, thread_cap, 0, 0, 0, 0, 0, 0, 0, 0);
93+
return syscall(SYS_get_affinity, thread_cap, 0, 0, 0, 0, 0, 0, 0);
7894
}
7995

8096
/*
@@ -84,99 +100,99 @@ s32 usys_get_affinity(u64 thread_cap)
84100

85101
u32 usys_get_cpu_id(void)
86102
{
87-
return (u32) syscall(SYS_get_cpu_id, 0, 0, 0, 0, 0, 0, 0, 0, 0);
103+
return (u32) syscall(SYS_get_cpu_id, 0, 0, 0, 0, 0, 0, 0, 0);
88104
}
89105

90106
int usys_create_thread(u64 process_cap, u64 stack, u64 pc, u64 arg, u32 prio,
91107
s32 aff)
92108
{
93109
return syscall(SYS_create_thread, process_cap, stack, pc, arg,
94-
(u64) prio, (u64) aff, 0, 0, 0);
110+
(u64) prio, (u64) aff, 0, 0);
95111
}
96112

97113
int usys_create_process(void)
98114
{
99-
return syscall(SYS_create_process, 0, 0, 0, 0, 0, 0, 0, 0, 0);
115+
return syscall(SYS_create_process, 0, 0, 0, 0, 0, 0, 0, 0);
100116
}
101117

102118
u64 usys_register_server(u64 callback, u64 max_client, u64 vm_config_ptr)
103119
{
104120
return syscall(SYS_register_server, callback, max_client, vm_config_ptr,
105-
0, 0, 0, 0, 0, 0);
121+
0, 0, 0, 0, 0);
106122
}
107123

108124
u32 usys_register_client(u32 server_cap, u64 vm_config_ptr)
109125
{
110126
return syscall(SYS_register_client, server_cap, vm_config_ptr,
111-
0, 0, 0, 0, 0, 0, 0);
127+
0, 0, 0, 0, 0, 0);
112128
}
113129

114130
u64 usys_ipc_call(u32 conn_cap, u64 arg0)
115131
{
116-
return syscall(SYS_ipc_call, conn_cap, arg0, 0, 0, 0, 0, 0, 0, 0);
132+
return syscall(SYS_ipc_call, conn_cap, arg0, 0, 0, 0, 0, 0, 0);
117133
}
118134

119135
u64 usys_ipc_reg_call(u32 conn_cap, u64 arg0)
120136
{
121-
return syscall(SYS_ipc_reg_call, conn_cap, arg0, 0, 0, 0, 0, 0, 0, 0);
137+
return syscall(SYS_ipc_reg_call, conn_cap, arg0, 0, 0, 0, 0, 0, 0);
122138
}
123139

124140
void usys_ipc_return(u64 ret)
125141
{
126-
syscall(SYS_ipc_return, ret, 0, 0, 0, 0, 0, 0, 0, 0);
142+
syscall(SYS_ipc_return, ret, 0, 0, 0, 0, 0, 0, 0);
127143
}
128144

129145
int usys_debug(void)
130146
{
131-
return syscall(SYS_debug, 0, 0, 0, 0, 0, 0, 0, 0, 0);
147+
return syscall(SYS_debug, 0, 0, 0, 0, 0, 0, 0, 0);
132148
}
133149

134150
int usys_cap_copy_to(u64 dest_process_cap, u64 src_slot_id)
135151
{
136152
return syscall(SYS_cap_copy_to, dest_process_cap, src_slot_id,
137-
0, 0, 0, 0, 0, 0, 0);
153+
0, 0, 0, 0, 0, 0);
138154
}
139155

140156
int usys_cap_copy_from(u64 src_process_cap, u64 src_slot_id)
141157
{
142158
return syscall(SYS_cap_copy_from, src_process_cap, src_slot_id,
143-
0, 0, 0, 0, 0, 0, 0);
159+
0, 0, 0, 0, 0, 0);
144160
}
145161

146162
int usys_fs_load_cpio(u64 vaddr)
147163
{
148-
return syscall(SYS_fs_load_cpio, vaddr, 0, 0, 0, 0, 0, 0, 0, 0);
164+
return syscall(SYS_fs_load_cpio, vaddr, 0, 0, 0, 0, 0, 0, 0);
149165
}
150166

151167
int usys_create_pmos(void *req, u64 cnt)
152168
{
153-
return syscall(SYS_create_pmos, (u64) req, cnt, 0, 0, 0, 0, 0, 0, 0);
169+
return syscall(SYS_create_pmos, (u64) req, cnt, 0, 0, 0, 0, 0, 0);
154170
}
155171

156172
int usys_map_pmos(u64 cap, void *req, u64 cnt)
157173
{
158-
return syscall(SYS_map_pmos, cap, (u64) req, cnt, 0, 0, 0, 0, 0, 0);
174+
return syscall(SYS_map_pmos, cap, (u64) req, cnt, 0, 0, 0, 0, 0);
159175
}
160176

161177
int usys_write_pmo(u64 cap, u64 offset, void *buf, u64 size)
162178
{
163179
return syscall(SYS_write_pmo, cap, offset, (u64) buf, size,
164-
0, 0, 0, 0, 0);
180+
0, 0, 0, 0);
165181
}
166182

167183
int usys_read_pmo(u64 cap, u64 offset, void *buf, u64 size)
168184
{
169185
return syscall(SYS_read_pmo, cap, offset, (u64) buf, size,
170-
0, 0, 0, 0, 0);
186+
0, 0, 0, 0);
171187
}
172188

173189
int usys_transfer_caps(u64 process, int *src_caps, int nr, int *dst_caps)
174190
{
175191
return syscall(SYS_transfer_caps, process, (u64) src_caps,
176-
(u64) nr, (u64) dst_caps, 0, 0, 0, 0, 0);
192+
(u64) nr, (u64) dst_caps, 0, 0, 0, 0);
177193
}
178194

179195
void usys_top(void)
180196
{
181-
syscall(SYS_top, 0, 0, 0, 0, 0, 0, 0, 0, 0);
197+
syscall(SYS_top, 0, 0, 0, 0, 0, 0, 0, 0);
182198
}

0 commit comments

Comments
 (0)