Added support for returning errors from syscalls
[akaros.git] / kern / src / syscall.c
1 /* See COPYRIGHT for copyright information. */
2
3 #ifdef __SHARC__
4 #pragma nosharc
5 #endif
6
7 #include <ros/common.h>
8 #include <arch/types.h>
9 #include <arch/arch.h>
10 #include <arch/mmu.h>
11 #include <arch/console.h>
12 #include <ros/timer.h>
13 #include <ros/error.h>
14
15 #include <elf.h>
16 #include <string.h>
17 #include <assert.h>
18 #include <process.h>
19 #include <schedule.h>
20 #include <pmap.h>
21 #include <mm.h>
22 #include <trap.h>
23 #include <syscall.h>
24 #include <kmalloc.h>
25 #include <stdio.h>
26 #include <resource.h>
27 #include <frontend.h>
28 #include <colored_caches.h>
29 #include <arch/bitmask.h>
30 #include <kfs.h> // eventually replace this with vfs.h
31
32
33 #ifdef __NETWORK__
34 #include <arch/nic_common.h>
35 extern int (*send_frame)(const char *CT(len) data, size_t len);
36 extern char device_mac[6];
37 #endif
38
39 /************** Utility Syscalls **************/
40
41 static int sys_null(void)
42 {
43         return 0;
44 }
45
46 // Writes 'val' to 'num_writes' entries of the well-known array in the kernel
47 // address space.  It's just #defined to be some random 4MB chunk (which ought
48 // to be boot_alloced or something).  Meant to grab exclusive access to cache
49 // lines, to simulate doing something useful.
50 static int sys_cache_buster(struct proc *p, uint32_t num_writes,
51                              uint32_t num_pages, uint32_t flags)
52 { TRUSTEDBLOCK /* zra: this is not really part of the kernel */
53         #define BUSTER_ADDR             0xd0000000  // around 512 MB deep
54         #define MAX_WRITES              1048576*8
55         #define MAX_PAGES               32
56         #define INSERT_ADDR     (UINFO + 2*PGSIZE) // should be free for these tests
57         uint32_t* buster = (uint32_t*)BUSTER_ADDR;
58         static spinlock_t buster_lock = SPINLOCK_INITIALIZER;
59         uint64_t ticks = -1;
60         page_t* a_page[MAX_PAGES];
61
62         /* Strided Accesses or Not (adjust to step by cachelines) */
63         uint32_t stride = 1;
64         if (flags & BUSTER_STRIDED) {
65                 stride = 16;
66                 num_writes *= 16;
67         }
68
69         /* Shared Accesses or Not (adjust to use per-core regions)
70          * Careful, since this gives 8MB to each core, starting around 512MB.
71          * Also, doesn't separate memory for core 0 if it's an async call.
72          */
73         if (!(flags & BUSTER_SHARED))
74                 buster = (uint32_t*)(BUSTER_ADDR + core_id() * 0x00800000);
75
76         /* Start the timer, if we're asked to print this info*/
77         if (flags & BUSTER_PRINT_TICKS)
78                 ticks = start_timing();
79
80         /* Allocate num_pages (up to MAX_PAGES), to simulate doing some more
81          * realistic work.  Note we don't write to these pages, even if we pick
82          * unshared.  Mostly due to the inconvenience of having to match up the
83          * number of pages with the number of writes.  And it's unnecessary.
84          */
85         if (num_pages) {
86                 spin_lock(&buster_lock);
87                 for (int i = 0; i < MIN(num_pages, MAX_PAGES); i++) {
88                         upage_alloc(p, &a_page[i],1);
89                         page_insert(p->env_pgdir, a_page[i], (void*)INSERT_ADDR + PGSIZE*i,
90                                     PTE_USER_RW);
91                 }
92                 spin_unlock(&buster_lock);
93         }
94
95         if (flags & BUSTER_LOCKED)
96                 spin_lock(&buster_lock);
97         for (int i = 0; i < MIN(num_writes, MAX_WRITES); i=i+stride)
98                 buster[i] = 0xdeadbeef;
99         if (flags & BUSTER_LOCKED)
100                 spin_unlock(&buster_lock);
101
102         if (num_pages) {
103                 spin_lock(&buster_lock);
104                 for (int i = 0; i < MIN(num_pages, MAX_PAGES); i++) {
105                         page_remove(p->env_pgdir, (void*)(INSERT_ADDR + PGSIZE * i));
106                         page_decref(a_page[i]);
107                 }
108                 spin_unlock(&buster_lock);
109         }
110
111         /* Print info */
112         if (flags & BUSTER_PRINT_TICKS) {
113                 ticks = stop_timing(ticks);
114                 printk("%llu,", ticks);
115         }
116         return 0;
117 }
118
119 static int sys_cache_invalidate(void)
120 {
121         #ifdef __i386__
122                 wbinvd();
123         #endif
124         return 0;
125 }
126
127 /* sys_reboot(): called directly from dispatch table. */
128
129 // Print a string to the system console.
130 // The string is exactly 'len' characters long.
131 // Destroys the environment on memory errors.
132 static ssize_t sys_cputs(env_t* e, const char *DANGEROUS s, size_t len)
133 {
134         // Check that the user has permission to read memory [s, s+len).
135         // Destroy the environment if not.
136         char *COUNT(len) _s = user_mem_assert(e, s, len, PTE_USER_RO);
137
138         // Print the string supplied by the user.
139         printk("%.*s", len, _s);
140         return (ssize_t)len;
141 }
142
143 // Read a character from the system console.
144 // Returns the character.
145 static uint16_t sys_cgetc(env_t* e)
146 {
147         uint16_t c;
148
149         // The cons_getc() primitive doesn't wait for a character,
150         // but the sys_cgetc() system call does.
151         while ((c = cons_getc()) == 0)
152                 cpu_relax();
153
154         return c;
155 }
156
157 /* Returns the id of the cpu this syscall is executed on. */
158 static uint32_t sys_getcpuid(void)
159 {
160         return core_id();
161 }
162
163 // TODO: Temporary hack until thread-local storage is implemented on i386
164 static size_t sys_getvcoreid(env_t* e)
165 {
166         if(e->state == PROC_RUNNING_S)
167                 return 0;
168
169         size_t i;
170         for(i = 0; i < e->num_vcores; i++)
171                 if(core_id() == e->vcoremap[i])
172                         return i;
173
174         panic("virtual core id not found in sys_getvcoreid()!");
175 }
176
177 /************** Process management syscalls **************/
178
179 /* Returns the calling process's pid */
180 static pid_t sys_getpid(struct proc *p)
181 {
182         return p->pid;
183 }
184
185 /*
186  * Creates a process found at the user string 'path'.  Currently uses KFS.
187  * Not runnable by default, so it needs it's status to be changed so that the
188  * next call to schedule() will try to run it.
189  * TODO: once we have a decent VFS, consider splitting this up
190  * and once there's an mmap, can have most of this in process.c
191  */
192 static int sys_proc_create(struct proc *p, const char *DANGEROUS path)
193 {
194         int pid = 0;
195         char tpath[MAX_PATH_LEN];
196         /*
197          * There's a bunch of issues with reading in the path, which we'll
198          * need to sort properly in the VFS.  Main concerns are TOCTOU (copy-in),
199          * whether or not it's a big deal that the pointer could be into kernel
200          * space, and resolving both of these without knowing the length of the
201          * string. (TODO)
202          * Change this so that all syscalls with a pointer take a length.
203          *
204          * zra: I've added this user_mem_strlcpy, which I think eliminates the
205      * the TOCTOU issue. Adding a length arg to this call would allow a more
206          * efficient implementation, though, since only one call to user_mem_check
207          * would be required.
208          */
209         int ret = user_mem_strlcpy(p,tpath, path, MAX_PATH_LEN, PTE_USER_RO);
210         int kfs_inode = kfs_lookup_path(tpath);
211         if (kfs_inode < 0)
212                 return -EINVAL;
213         struct proc *new_p = kfs_proc_create(kfs_inode);
214         pid = new_p->pid;
215         proc_decref(new_p, 1); // let go of the reference created in proc_create()
216         return pid;
217 }
218
219 /* Makes process PID runnable.  Consider moving the functionality to process.c */
220 static error_t sys_proc_run(struct proc *p, unsigned pid)
221 {
222         struct proc *target = pid2proc(pid);
223         error_t retval = 0;
224
225         if (!target)
226                 return -EBADPROC;
227         // note we can get interrupted here. it's not bad.
228         spin_lock_irqsave(&p->proc_lock);
229         // make sure we have access and it's in the right state to be activated
230         if (!proc_controls(p, target)) {
231                 proc_decref(target, 1);
232                 retval = -EPERM;
233         } else if (target->state != PROC_CREATED) {
234                 proc_decref(target, 1);
235                 retval = -EINVAL;
236         } else {
237                 __proc_set_state(target, PROC_RUNNABLE_S);
238                 schedule_proc(target);
239         }
240         spin_unlock_irqsave(&p->proc_lock);
241         proc_decref(target, 1);
242         return retval;
243 }
244
245 /* Destroy proc pid.  If this is called by the dying process, it will never
246  * return.  o/w it will return 0 on success, or an error.  Errors include:
247  * - EBADPROC: if there is no such process with pid
248  * - EPERM: if caller does not control pid */
249 static error_t sys_proc_destroy(struct proc *p, pid_t pid, int exitcode)
250 {
251         error_t r;
252         struct proc *p_to_die = pid2proc(pid);
253
254         if (!p_to_die)
255                 return -EBADPROC;
256         if (!proc_controls(p, p_to_die)) {
257                 proc_decref(p_to_die, 1);
258                 return -EPERM;
259         }
260         if (p_to_die == p) {
261                 // syscall code and pid2proc both have edible references, only need 1.
262                 p->exitcode = exitcode;
263                 proc_decref(p, 1);
264                 printd("[PID %d] proc exiting gracefully (code %d)\n", p->pid,exitcode);
265         } else {
266                 panic("Destroying other processes is not supported yet.");
267                 //printk("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
268         }
269         proc_destroy(p_to_die);
270         return ESUCCESS;
271 }
272
273 static int sys_proc_yield(struct proc *p)
274 {
275         proc_yield(p);
276         return 0;
277 }
278
279 static ssize_t sys_run_binary(env_t* e, void *DANGEROUS binary_buf, size_t len,
280                               procinfo_t*DANGEROUS procinfo, size_t num_colors)
281 {
282         env_t* env = proc_create(NULL,0);
283         assert(env != NULL);
284
285         if(memcpy_from_user(e,e->env_procinfo,procinfo,sizeof(*procinfo)))
286                 return -1;
287         proc_init_procinfo(e);
288
289         env_load_icode(env,e,binary_buf,len);
290         __proc_set_state(env, PROC_RUNNABLE_S);
291         schedule_proc(env);
292         if(num_colors > 0) {
293                 env->cache_colors_map = cache_colors_map_alloc();
294                 for(int i=0; i<num_colors; i++)
295                         cache_color_alloc(llc_cache, env->cache_colors_map);
296         }
297         proc_decref(env, 1);
298         proc_yield(e);
299         return 0;
300 }
301
302 static ssize_t sys_fork(env_t* e)
303 {
304         // TODO: right now we only support fork for single-core processes
305         if(e->state != PROC_RUNNING_S)
306         {
307                 set_errno(current_tf,EINVAL);
308                 return -1;
309         }
310
311         env_t* env = proc_create(NULL,0);
312         assert(env != NULL);
313
314         env->heap_bottom = e->heap_bottom;
315         env->heap_top = e->heap_top;
316         env->ppid = e->pid;
317         env->env_tf = *current_tf;
318
319         env->cache_colors_map = cache_colors_map_alloc();
320         for(int i=0; i < llc_cache->num_colors; i++)
321                 if(GET_BITMASK_BIT(e->cache_colors_map,i))
322                         cache_color_alloc(llc_cache, env->cache_colors_map);
323
324         int copy_page(env_t* e, pte_t* pte, void* va, void* arg)
325         {
326                 env_t* env = (env_t*)arg;
327
328                 page_t* pp;
329                 if(upage_alloc(env,&pp,0))
330                         return -1;
331                 if(page_insert(env->env_pgdir,pp,va,*pte & PTE_PERM))
332                 {
333                         page_decref(pp);
334                         return -1;
335                 }
336
337                 pagecopy(page2kva(pp),ppn2kva(PTE2PPN(*pte)));
338                 return 0;
339         }
340
341         // copy procdata and procinfo
342         memcpy(env->env_procdata,e->env_procdata,sizeof(struct procdata));
343         memcpy(env->env_procinfo,e->env_procinfo,sizeof(struct procinfo));
344         env->env_procinfo->pid = env->pid;
345         env->env_procinfo->ppid = env->ppid;
346
347         // copy all memory below procdata
348         if(env_user_mem_walk(e,0,UDATA,&copy_page,env))
349         {
350                 proc_decref(env,2);
351                 set_errno(current_tf,ENOMEM);
352                 return -1;
353         }
354
355         __proc_set_state(env, PROC_RUNNABLE_S);
356         schedule_proc(env);
357
358         // don't decref the new process.
359         // that will happen when the parent waits for it.
360
361         printd("[PID %d] fork PID %d\n",e->pid,env->pid);
362
363         return env->pid;
364 }
365
366 intreg_t sys_exec(struct proc* p, const char fn[MAX_PATH_LEN], procinfo_t* pi)
367 {
368         if(p->state != PROC_RUNNING_S)
369                 return -1;
370
371         int ret = -1;
372         char* kfn = kmalloc(MAX_PATH_LEN,0);
373         if(kfn == NULL)
374                 goto out;
375         if(memcpy_from_user(p,kfn,fn,MAX_PATH_LEN))
376                 goto out;
377
378         if(memcpy_from_user(p,p->env_procinfo,pi,sizeof(procinfo_t)))
379         {
380                 proc_destroy(p);
381                 goto out;
382         }
383         proc_init_procinfo(p);
384
385         env_segment_free(p,0,USTACKTOP);
386
387         if(load_elf(p,kfn))
388         {
389                 proc_destroy(p);
390                 goto out;
391         }
392         *current_tf = p->env_tf;
393         ret = 0;
394
395         printd("[PID %d] exec %s\n",p->pid,kfn);
396
397 out:
398         kfree(kfn);
399         return ret;
400 }
401
402 static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
403 {
404         struct proc* p = pid2proc(pid);
405
406         // TODO: this syscall is racy, so we only support for single-core procs
407         if(e->state != PROC_RUNNING_S)
408                 return -1;
409
410         // TODO: need to use errno properly.  sadly, ROS error codes conflict..
411
412         if(p)
413         {
414                 ssize_t ret;
415
416                 if(current->pid == p->ppid)
417                 {
418                         if(p->state == PROC_DYING)
419                         {
420                                 memcpy_to_user(e,status,&p->exitcode,sizeof(int));
421                                 printd("[PID %d] waited for PID %d (code %d)\n",
422                                        e->pid,p->pid,p->exitcode);
423                                 ret = 0;
424                         }
425                         else // not dead yet
426                         {
427                                 set_errno(current_tf,0);
428                                 ret = -1;
429                         }
430                 }
431                 else // not a child of the calling process
432                 {
433                         set_errno(current_tf,1);
434                         ret = -1;
435                 }
436
437                 // if the wait succeeded, decref twice
438                 proc_decref(p,1 + (ret == 0));
439                 return ret;
440         }
441
442         set_errno(current_tf,1);
443         return -1;
444 }
445
446 /************** Memory Management Syscalls **************/
447
448 static void *sys_mmap(struct proc* p, uintreg_t a1, uintreg_t a2, uintreg_t a3,
449                       uintreg_t* a456)
450 {
451         uintreg_t _a456[3];
452         if(memcpy_from_user(p,_a456,a456,3*sizeof(uintreg_t)))
453                 sys_proc_destroy(p,p->pid,-1);
454         return mmap(p,a1,a2,a3,_a456[0],_a456[1],_a456[2]);
455 }
456
457 static intreg_t sys_mprotect(struct proc* p, void* addr, size_t len, int prot)
458 {
459         return mprotect(p, addr, len, prot);
460 }
461
462 static intreg_t sys_munmap(struct proc* p, void* addr, size_t len)
463 {
464         return munmap(p, addr, len);
465 }
466
467 static void* sys_brk(struct proc *p, void* addr) {
468         size_t range;
469
470         spin_lock_irqsave(&p->proc_lock);
471
472         if((addr < p->heap_bottom) || (addr >= (void*)USTACKBOT))
473                 goto out;
474
475         if (addr > p->heap_top) {
476                 range = addr - p->heap_top;
477                 env_segment_alloc(p, p->heap_top, range);
478         }
479         else if (addr < p->heap_top) {
480                 range = p->heap_top - addr;
481                 env_segment_free(p, addr, range);
482         }
483         p->heap_top = addr;
484
485 out:
486         spin_unlock_irqsave(&p->proc_lock);
487         return p->heap_top;
488 }
489
490 static ssize_t sys_shared_page_alloc(env_t* p1,
491                                      void**DANGEROUS _addr, pid_t p2_id,
492                                      int p1_flags, int p2_flags
493                                     )
494 {
495         //if (!VALID_USER_PERMS(p1_flags)) return -EPERM;
496         //if (!VALID_USER_PERMS(p2_flags)) return -EPERM;
497
498         void * COUNT(1) * COUNT(1) addr = user_mem_assert(p1, _addr, sizeof(void *),
499                                                       PTE_USER_RW);
500         struct proc *p2 = pid2proc(p2_id);
501         if (!p2)
502                 return -EBADPROC;
503
504         page_t* page;
505         error_t e = upage_alloc(p1, &page,1);
506         if (e < 0) {
507                 proc_decref(p2, 1);
508                 return e;
509         }
510
511         void* p2_addr = page_insert_in_range(p2->env_pgdir, page,
512                         (void*SNT)UTEXT, (void*SNT)UTOP, p2_flags);
513         if (p2_addr == NULL) {
514                 page_free(page);
515                 proc_decref(p2, 1);
516                 return -EFAIL;
517         }
518
519         void* p1_addr = page_insert_in_range(p1->env_pgdir, page,
520                         (void*SNT)UTEXT, (void*SNT)UTOP, p1_flags);
521         if(p1_addr == NULL) {
522                 page_remove(p2->env_pgdir, p2_addr);
523                 page_free(page);
524                 proc_decref(p2, 1);
525                 return -EFAIL;
526         }
527         *addr = p1_addr;
528         proc_decref(p2, 1);
529         return ESUCCESS;
530 }
531
532 static int sys_shared_page_free(env_t* p1, void*DANGEROUS addr, pid_t p2)
533 {
534         return -1;
535 }
536
537
538 /************** Resource Request Syscalls **************/
539
540 /* sys_resource_req(): called directly from dispatch table. */
541
542 /************** Platform Specific Syscalls **************/
543
544 //Read a buffer over the serial port
545 static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
546 {
547         if (len == 0)
548                 return 0;
549
550         #ifdef SERIAL_IO
551             char *COUNT(len) buf = user_mem_assert(e, _buf, len, PTE_USER_RO);
552                 size_t bytes_read = 0;
553                 int c;
554                 while((c = serial_read_byte()) != -1) {
555                         buf[bytes_read++] = (uint8_t)c;
556                         if(bytes_read == len) break;
557                 }
558                 return (ssize_t)bytes_read;
559         #else
560                 return -EINVAL;
561         #endif
562 }
563
564 //Write a buffer over the serial port
565 static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
566 {
567         if (len == 0)
568                 return 0;
569         #ifdef SERIAL_IO
570                 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_USER_RO);
571                 for(int i =0; i<len; i++)
572                         serial_send_byte(buf[i]);
573                 return (ssize_t)len;
574         #else
575                 return -EINVAL;
576         #endif
577 }
578
579 #ifdef __NETWORK__
580 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
581 static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
582 {
583         if (eth_up) {
584
585                 uint32_t len;
586                 char *ptr;
587
588                 spin_lock(&packet_buffers_lock);
589
590                 if (num_packet_buffers == 0) {
591                         spin_unlock(&packet_buffers_lock);
592                         return 0;
593                 }
594
595                 ptr = packet_buffers[packet_buffers_head];
596                 len = packet_buffers_sizes[packet_buffers_head];
597
598                 num_packet_buffers--;
599                 packet_buffers_head = (packet_buffers_head + 1) % MAX_PACKET_BUFFERS;
600
601                 spin_unlock(&packet_buffers_lock);
602
603                 char* _buf = user_mem_assert(e, buf, len, PTE_U);
604
605                 memcpy(_buf, ptr, len);
606
607                 kfree(ptr);
608
609                 return len;
610         }
611         else
612                 return -EINVAL;
613 }
614
615 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
616 static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
617 {
618         if (eth_up) {
619
620                 if (len == 0)
621                         return 0;
622
623                 // HACK TO BYPASS HACK
624                 int just_sent = send_frame(buf, len);
625
626                 if (just_sent < 0) {
627                         printk("Packet send fail\n");
628                         return 0;
629                 }
630
631                 return just_sent;
632
633                 // END OF RECURSIVE HACK
634 /*
635                 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_U);
636                 int total_sent = 0;
637                 int just_sent = 0;
638                 int cur_packet_len = 0;
639                 while (total_sent != len) {
640                         cur_packet_len = ((len - total_sent) > MTU) ? MTU : (len - total_sent);
641                         char dest_mac[6] = APPSERVER_MAC_ADDRESS;
642                         char* wrap_buffer = eth_wrap(_buf + total_sent, cur_packet_len, device_mac, dest_mac, APPSERVER_PORT);
643                         just_sent = send_frame(wrap_buffer, cur_packet_len + sizeof(struct ETH_Header));
644
645                         if (just_sent < 0)
646                                 return 0; // This should be an error code of its own
647
648                         if (wrap_buffer)
649                                 kfree(wrap_buffer);
650
651                         total_sent += cur_packet_len;
652                 }
653
654                 return (ssize_t)len;
655 */
656         }
657         else
658                 return -EINVAL;
659 }
660
661 static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf) 
662 {
663         if (eth_up) {
664                 for (int i = 0; i < 6; i++)
665                         buf[i] = device_mac[i];
666                 return 0;
667         }
668         else
669                 return -EINVAL;
670 }
671
672 static int sys_eth_recv_check(env_t* e) 
673 {
674         if (num_packet_buffers != 0) 
675                 return 1;
676         else
677                 return 0;
678 }
679
680 #endif // Network
681
682 // Syscalls below here are serviced by the appserver for now.
683 #define ufe(which,a0,a1,a2,a3) \
684         frontend_syscall_errno(p,APPSERVER_SYSCALL_##which,\
685                            (int)(a0),(int)(a1),(int)(a2),(int)(a3))
686
687 intreg_t sys_write(struct proc* p, int fd, const void* buf, int len)
688 {
689         void* kbuf = user_memdup_errno(p,buf,len);
690         if(kbuf == NULL)
691                 return -1;
692         int ret = ufe(write,fd,PADDR(kbuf),len,0);
693         user_memdup_free(p,kbuf);
694         return ret;
695 }
696
697 intreg_t sys_read(struct proc* p, int fd, void* buf, int len)
698 {
699         void* kbuf = kmalloc_errno(len);
700         if(kbuf == NULL)
701                 return -1;
702         int ret = ufe(read,fd,PADDR(kbuf),len,0);
703         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,len))
704                 ret = -1;
705         user_memdup_free(p,kbuf);
706         return ret;
707 }
708
709 intreg_t sys_pwrite(struct proc* p, int fd, const void* buf, int len, int offset)
710 {
711         void* kbuf = user_memdup_errno(p,buf,len);
712         if(kbuf == NULL)
713                 return -1;
714         int ret = ufe(pwrite,fd,PADDR(kbuf),len,offset);
715         user_memdup_free(p,kbuf);
716         return ret;
717 }
718
719 intreg_t sys_pread(struct proc* p, int fd, void* buf, int len, int offset)
720 {
721         void* kbuf = kmalloc_errno(len);
722         if(kbuf == NULL)
723                 return -1;
724         int ret = ufe(pread,fd,PADDR(kbuf),len,offset);
725         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,len))
726                 ret = -1;
727         user_memdup_free(p,kbuf);
728         return ret;
729 }
730
731 intreg_t sys_open(struct proc* p, const char* path, int oflag, int mode)
732 {
733         char* fn = user_strdup_errno(p,path,PGSIZE);
734         if(fn == NULL)
735                 return -1;
736         int ret = ufe(open,PADDR(fn),oflag,mode,0);
737         user_memdup_free(p,fn);
738         return ret;
739 }
740 intreg_t sys_close(struct proc* p, int fd)
741 {
742         return ufe(close,fd,0,0,0);
743 }
744
745 #define NEWLIB_STAT_SIZE 64
746 intreg_t sys_fstat(struct proc* p, int fd, void* buf)
747 {
748         int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
749         int ret = ufe(fstat,fd,PADDR(kbuf),0,0);
750         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
751                 ret = -1;
752         kfree(kbuf);
753         return ret;
754 }
755
756 intreg_t sys_stat(struct proc* p, const char* path, void* buf)
757 {
758         char* fn = user_strdup_errno(p,path,PGSIZE);
759         if(fn == NULL)
760                 return -1;
761
762         int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
763         int ret = ufe(stat,PADDR(fn),PADDR(kbuf),0,0);
764         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
765                 ret = -1;
766
767         user_memdup_free(p,fn);
768         kfree(kbuf);
769         return ret;
770 }
771
772 intreg_t sys_lstat(struct proc* p, const char* path, void* buf)
773 {
774         char* fn = user_strdup_errno(p,path,PGSIZE);
775         if(fn == NULL)
776                 return -1;
777
778         int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
779         int ret = ufe(lstat,PADDR(fn),PADDR(kbuf),0,0);
780         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
781                 ret = -1;
782
783         user_memdup_free(p,fn);
784         kfree(kbuf);
785         return ret;
786 }
787
788 intreg_t sys_fcntl(struct proc* p, int fd, int cmd, int arg)
789 {
790         return ufe(fcntl,fd,cmd,arg,0);
791 }
792
793 intreg_t sys_access(struct proc* p, const char* path, int type)
794 {
795         char* fn = user_strdup_errno(p,path,PGSIZE);
796         if(fn == NULL)
797                 return -1;
798         int ret = ufe(access,PADDR(fn),type,0,0);
799         user_memdup_free(p,fn);
800         return ret;
801 }
802
803 intreg_t sys_umask(struct proc* p, int mask)
804 {
805         return ufe(umask,mask,0,0,0);
806 }
807
808 intreg_t sys_chmod(struct proc* p, const char* path, int mode)
809 {
810         char* fn = user_strdup_errno(p,path,PGSIZE);
811         if(fn == NULL)
812                 return -1;
813         int ret = ufe(chmod,PADDR(fn),mode,0,0);
814         user_memdup_free(p,fn);
815         return ret;
816 }
817
818 intreg_t sys_lseek(struct proc* p, int fd, int offset, int whence)
819 {
820         return ufe(lseek,fd,offset,whence,0);
821 }
822
823 intreg_t sys_link(struct proc* p, const char* _old, const char* _new)
824 {
825         char* oldpath = user_strdup_errno(p,_old,PGSIZE);
826         if(oldpath == NULL)
827                 return -1;
828
829         char* newpath = user_strdup_errno(p,_new,PGSIZE);
830         if(newpath == NULL)
831         {
832                 user_memdup_free(p,oldpath);
833                 return -1;
834         }
835
836         int ret = ufe(link,PADDR(oldpath),PADDR(newpath),0,0);
837         user_memdup_free(p,oldpath);
838         user_memdup_free(p,newpath);
839         return ret;
840 }
841
842 intreg_t sys_unlink(struct proc* p, const char* path)
843 {
844         char* fn = user_strdup_errno(p,path,PGSIZE);
845         if(fn == NULL)
846                 return -1;
847         int ret = ufe(unlink,PADDR(fn),0,0,0);
848         user_memdup_free(p,fn);
849         return ret;
850 }
851
852 intreg_t sys_chdir(struct proc* p, const char* path)
853 {
854         char* fn = user_strdup_errno(p,path,PGSIZE);
855         if(fn == NULL)
856                 return -1;
857         int ret = ufe(chdir,PADDR(fn),0,0,0);
858         user_memdup_free(p,fn);
859         return ret;
860 }
861
862 intreg_t sys_getcwd(struct proc* p, char* pwd, int size)
863 {
864         void* kbuf = kmalloc_errno(size);
865         if(kbuf == NULL)
866                 return -1;
867         int ret = ufe(read,PADDR(kbuf),size,0,0);
868         if(ret != -1 && memcpy_to_user_errno(p,pwd,kbuf,strnlen(kbuf,size)))
869                 ret = -1;
870         user_memdup_free(p,kbuf);
871         return ret;
872 }
873
874 intreg_t sys_gettimeofday(struct proc* p, int* buf)
875 {
876         static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
877         static int t0 = 0;
878
879         spin_lock(&gtod_lock);
880         if(t0 == 0)
881                 t0 = ufe(time,0,0,0,0);
882         spin_unlock(&gtod_lock);
883
884         long long dt = read_tsc();
885         int kbuf[2] = {t0+dt/system_timing.tsc_freq,
886             (dt%system_timing.tsc_freq)*1000000/system_timing.tsc_freq};
887
888         return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
889 }
890
891 #define SIZEOF_STRUCT_TERMIOS 60
892 intreg_t sys_tcgetattr(struct proc* p, int fd, void* termios_p)
893 {
894         int* kbuf = kmalloc(SIZEOF_STRUCT_TERMIOS,0);
895         int ret = ufe(tcgetattr,fd,PADDR(kbuf),0,0);
896         if(ret != -1 && memcpy_to_user_errno(p,termios_p,kbuf,SIZEOF_STRUCT_TERMIOS))
897                 ret = -1;
898         kfree(kbuf);
899         return ret;
900 }
901
902 intreg_t sys_tcsetattr(struct proc* p, int fd, int optional_actions, const void* termios_p)
903 {
904         void* kbuf = user_memdup_errno(p,termios_p,SIZEOF_STRUCT_TERMIOS);
905         if(kbuf == NULL)
906                 return -1;
907         int ret = ufe(tcsetattr,fd,optional_actions,PADDR(kbuf),0);
908         user_memdup_free(p,kbuf);
909         return ret;
910 }
911 /************** Syscall Invokation **************/
912
913 /* Executes the given syscall.
914  *
915  * Note tf is passed in, which points to the tf of the context on the kernel
916  * stack.  If any syscall needs to block, it needs to save this info, as well as
917  * any silly state.
918  *
919  * TODO: Build a dispatch table instead of switching on the syscallno
920  * Dispatches to the correct kernel function, passing the arguments.
921  */
922 intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
923                  uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
924 {
925         // Initialize the return value and error code returned to 0
926         proc_set_syscall_retval(current_tf, 0);
927         set_errno(current_tf,0);
928
929         typedef intreg_t (*syscall_t)(struct proc*,uintreg_t,uintreg_t,
930                                       uintreg_t,uintreg_t,uintreg_t);
931
932         const static syscall_t syscall_table[] = {
933                 [SYS_null] = (syscall_t)sys_null,
934                 [SYS_cache_buster] = (syscall_t)sys_cache_buster,
935                 [SYS_cache_invalidate] = (syscall_t)sys_cache_invalidate,
936                 [SYS_reboot] = (syscall_t)reboot,
937                 [SYS_cputs] = (syscall_t)sys_cputs,
938                 [SYS_cgetc] = (syscall_t)sys_cgetc,
939                 [SYS_getcpuid] = (syscall_t)sys_getcpuid,
940                 [SYS_getvcoreid] = (syscall_t)sys_getvcoreid,
941                 [SYS_getpid] = (syscall_t)sys_getpid,
942                 [SYS_proc_create] = (syscall_t)sys_proc_create,
943                 [SYS_proc_run] = (syscall_t)sys_proc_run,
944                 [SYS_proc_destroy] = (syscall_t)sys_proc_destroy,
945                 [SYS_yield] = (syscall_t)sys_proc_yield,
946                 [SYS_run_binary] = (syscall_t)sys_run_binary,
947                 [SYS_fork] = (syscall_t)sys_fork,
948                 [SYS_exec] = (syscall_t)sys_exec,
949                 [SYS_trywait] = (syscall_t)sys_trywait,
950                 [SYS_mmap] = (syscall_t)sys_mmap,
951                 [SYS_munmap] = (syscall_t)sys_munmap,
952                 [SYS_mprotect] = (syscall_t)sys_mprotect,
953                 [SYS_brk] = (syscall_t)sys_brk,
954                 [SYS_shared_page_alloc] = (syscall_t)sys_shared_page_alloc,
955                 [SYS_shared_page_free] = (syscall_t)sys_shared_page_free,
956                 [SYS_resource_req] = (syscall_t)resource_req,
957         #ifdef __i386__
958                 [SYS_serial_read] = (syscall_t)sys_serial_read,
959                 [SYS_serial_write] = (syscall_t)sys_serial_write,
960         #endif
961         #ifdef __NETWORK__
962                 [SYS_eth_read] = (syscall_t)sys_eth_read,
963                 [SYS_eth_write] = (syscall_t)sys_eth_write,
964                 [SYS_eth_get_mac_addr] = (syscall_t)sys_eth_get_mac_addr,
965                 [SYS_eth_recv_check] = (syscall_t)sys_eth_recv_check,
966         #endif
967                 // Syscalls serviced by the appserver for now.
968                 [SYS_read] = (syscall_t)sys_read,
969                 [SYS_write] = (syscall_t)sys_write,
970                 [SYS_open] = (syscall_t)sys_open,
971                 [SYS_close] = (syscall_t)sys_close,
972                 [SYS_fstat] = (syscall_t)sys_fstat,
973                 [SYS_stat] = (syscall_t)sys_stat,
974                 [SYS_lstat] = (syscall_t)sys_lstat,
975                 [SYS_fcntl] = (syscall_t)sys_fcntl,
976                 [SYS_access] = (syscall_t)sys_access,
977                 [SYS_umask] = (syscall_t)sys_umask,
978                 [SYS_chmod] = (syscall_t)sys_chmod,
979                 [SYS_lseek] = (syscall_t)sys_lseek,
980                 [SYS_link] = (syscall_t)sys_link,
981                 [SYS_unlink] = (syscall_t)sys_unlink,
982                 [SYS_chdir] = (syscall_t)sys_chdir,
983                 [SYS_getcwd] = (syscall_t)sys_getcwd,
984                 [SYS_gettimeofday] = (syscall_t)sys_gettimeofday,
985                 [SYS_tcgetattr] = (syscall_t)sys_tcgetattr,
986                 [SYS_tcsetattr] = (syscall_t)sys_tcsetattr
987         };
988
989         const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
990
991         //printk("Incoming syscall on core: %d number: %d\n    a1: %x\n   "
992         //       " a2: %x\n    a3: %x\n    a4: %x\n    a5: %x\n", core_id(),
993         //       syscallno, a1, a2, a3, a4, a5);
994
995         if(syscallno > max_syscall || syscall_table[syscallno] == NULL)
996                 panic("Invalid syscall number %d for proc %x!", syscallno, *p);
997
998         return syscall_table[syscallno](p,a1,a2,a3,a4,a5);
999 }
1000
1001 intreg_t syscall_async(struct proc *p, syscall_req_t *call)
1002 {
1003         return syscall(p, call->num, call->args[0], call->args[1],
1004                        call->args[2], call->args[3], call->args[4]);
1005 }
1006
1007 /* You should already have a refcnt'd ref to p before calling this */
1008 intreg_t process_generic_syscalls(struct proc *p, size_t max)
1009 {
1010         size_t count = 0;
1011         syscall_back_ring_t* sysbr = &p->syscallbackring;
1012
1013         /* make sure the proc is still alive, and keep it from dying from under us
1014          * incref will return ESUCCESS on success.  This might need some thought
1015          * regarding when the incref should have happened (like by whoever passed us
1016          * the *p). */
1017         // TODO: ought to be unnecessary, if you called this right, kept here for
1018         // now in case anyone actually uses the ARSCs.
1019         proc_incref(p, 1);
1020
1021         // max is the most we'll process.  max = 0 means do as many as possible
1022         while (RING_HAS_UNCONSUMED_REQUESTS(sysbr) && ((!max)||(count < max)) ) {
1023                 if (!count) {
1024                         // ASSUME: one queue per process
1025                         // only switch cr3 for the very first request for this queue
1026                         // need to switch to the right context, so we can handle the user pointer
1027                         // that points to a data payload of the syscall
1028                         lcr3(p->env_cr3);
1029                 }
1030                 count++;
1031                 //printk("DEBUG PRE: sring->req_prod: %d, sring->rsp_prod: %d\n",
1032                 //         sysbr->sring->req_prod, sysbr->sring->rsp_prod);
1033                 // might want to think about 0-ing this out, if we aren't
1034                 // going to explicitly fill in all fields
1035                 syscall_rsp_t rsp;
1036                 // this assumes we get our answer immediately for the syscall.
1037                 syscall_req_t* req = RING_GET_REQUEST(sysbr, ++(sysbr->req_cons));
1038                 rsp.retval = syscall_async(p, req);
1039                 // write response into the slot it came from
1040                 memcpy(req, &rsp, sizeof(syscall_rsp_t));
1041                 // update our counter for what we've produced (assumes we went in order!)
1042                 (sysbr->rsp_prod_pvt)++;
1043                 RING_PUSH_RESPONSES(sysbr);
1044                 //printk("DEBUG POST: sring->req_prod: %d, sring->rsp_prod: %d\n",
1045                 //         sysbr->sring->req_prod, sysbr->sring->rsp_prod);
1046         }
1047         // load sane page tables (and don't rely on decref to do it for you).
1048         lcr3(boot_cr3);
1049         proc_decref(p, 1);
1050         return (intreg_t)count;
1051 }
1052