Fixes for fork and exec
[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         user_frontend_syscall(p,APPSERVER_SYSCALL_##which,\
685                            (int)(a0),(int)(a1),(int)(a2),(int)(a3))
686
687 int32_t sys_frontend(env_t* p, int32_t syscall_num, 
688                      uint32_t arg0, uint32_t arg1, 
689                      uint32_t arg2, uint32_t translate_args)
690 {
691         // really, we just want to pin pages, but irqdisable works
692         static spinlock_t lock = SPINLOCK_INITIALIZER;
693         spin_lock_irqsave(&lock);
694
695         uint32_t arg[3] = {arg0,arg1,arg2};
696         for(int i = 0; i < 3; i++)
697         {
698                 int flags = (translate_args & (1 << (i+3))) ? PTE_USER_RW :
699                            ((translate_args & (1 << i)) ? PTE_USER_RO : 0);
700                 if(flags)
701                 {
702                         pte_t* pte = pgdir_walk(p->env_pgdir,(void*)arg[i],0);
703                         if(pte == NULL || !(*pte & flags))
704                         {
705                                 spin_unlock_irqsave(&lock);
706                                 return -1;
707                         }
708                         arg[i] = PTE_ADDR(*pte) | PGOFF(arg[i]);
709                 }
710         }
711
712         int32_t ret = user_frontend_syscall(p,syscall_num,arg[0],arg[1],arg[2],0);
713
714         spin_unlock_irqsave(&lock);
715         return ret;
716 }
717
718
719 intreg_t sys_write(struct proc* p, int fd, const void* buf, int len)
720 {
721         void* kbuf = user_memdup_errno(p,buf,len);
722         if(kbuf == NULL)
723                 return -1;
724         int ret = ufe(write,fd,PADDR(kbuf),len,0);
725         user_memdup_free(p,kbuf);
726         return ret;
727 }
728
729 intreg_t sys_read(struct proc* p, int fd, void* buf, int len)
730 {
731         void* kbuf = kmalloc_errno(len);
732         if(kbuf == NULL)
733                 return -1;
734         int ret = ufe(read,fd,PADDR(kbuf),len,0);
735         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,len))
736                 ret = -1;
737         user_memdup_free(p,kbuf);
738         return ret;
739 }
740
741 intreg_t sys_pwrite(struct proc* p, int fd, const void* buf, int len, int offset)
742 {
743         void* kbuf = user_memdup_errno(p,buf,len);
744         if(kbuf == NULL)
745                 return -1;
746         int ret = ufe(pwrite,fd,PADDR(kbuf),len,offset);
747         user_memdup_free(p,kbuf);
748         return ret;
749 }
750
751 intreg_t sys_pread(struct proc* p, int fd, void* buf, int len, int offset)
752 {
753         void* kbuf = kmalloc_errno(len);
754         if(kbuf == NULL)
755                 return -1;
756         int ret = ufe(pread,fd,PADDR(kbuf),len,offset);
757         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,len))
758                 ret = -1;
759         user_memdup_free(p,kbuf);
760         return ret;
761 }
762
763 intreg_t sys_open(struct proc* p, const char* path, int oflag, int mode)
764 {
765         char* fn = user_strdup_errno(p,path,PGSIZE);
766         if(fn == NULL)
767                 return -1;
768         int ret = ufe(open,PADDR(fn),oflag,mode,0);
769         user_memdup_free(p,fn);
770         return ret;
771 }
772 intreg_t sys_close(struct proc* p, int fd)
773 {
774         return ufe(close,fd,0,0,0);
775 }
776
777 #define NEWLIB_STAT_SIZE 64
778 intreg_t sys_fstat(struct proc* p, int fd, void* buf)
779 {
780         int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
781         int ret = ufe(fstat,fd,PADDR(kbuf),0,0);
782         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
783                 ret = -1;
784         kfree(kbuf);
785         return ret;
786 }
787
788 intreg_t sys_stat(struct proc* p, const char* path, void* buf)
789 {
790         char* fn = user_strdup_errno(p,path,PGSIZE);
791         if(fn == NULL)
792                 return -1;
793
794         int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
795         int ret = ufe(stat,PADDR(fn),PADDR(kbuf),0,0);
796         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
797                 ret = -1;
798
799         user_memdup_free(p,fn);
800         kfree(kbuf);
801         return ret;
802 }
803
804 intreg_t sys_lstat(struct proc* p, const char* path, void* buf)
805 {
806         char* fn = user_strdup_errno(p,path,PGSIZE);
807         if(fn == NULL)
808                 return -1;
809
810         int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
811         int ret = ufe(lstat,PADDR(fn),PADDR(kbuf),0,0);
812         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
813                 ret = -1;
814
815         user_memdup_free(p,fn);
816         kfree(kbuf);
817         return ret;
818 }
819
820 intreg_t sys_fcntl(struct proc* p, int fd, int cmd, int arg)
821 {
822         return ufe(fcntl,fd,cmd,arg,0);
823 }
824
825 intreg_t sys_access(struct proc* p, const char* path, int type)
826 {
827         char* fn = user_strdup_errno(p,path,PGSIZE);
828         if(fn == NULL)
829                 return -1;
830         int ret = ufe(access,PADDR(fn),type,0,0);
831         user_memdup_free(p,fn);
832         return ret;
833 }
834
835 intreg_t sys_umask(struct proc* p, int mask)
836 {
837         return ufe(umask,mask,0,0,0);
838 }
839
840 intreg_t sys_chmod(struct proc* p, const char* path, int mode)
841 {
842         char* fn = user_strdup_errno(p,path,PGSIZE);
843         if(fn == NULL)
844                 return -1;
845         int ret = ufe(chmod,PADDR(fn),mode,0,0);
846         user_memdup_free(p,fn);
847         return ret;
848 }
849
850 intreg_t sys_lseek(struct proc* p, int fd, int offset, int whence)
851 {
852         return ufe(lseek,fd,offset,whence,0);
853 }
854
855 intreg_t sys_link(struct proc* p, const char* _old, const char* _new)
856 {
857         char* oldpath = user_strdup_errno(p,_old,PGSIZE);
858         if(oldpath == NULL)
859                 return -1;
860
861         char* newpath = user_strdup_errno(p,_new,PGSIZE);
862         if(newpath == NULL)
863         {
864                 user_memdup_free(p,oldpath);
865                 return -1;
866         }
867
868         int ret = ufe(link,PADDR(oldpath),PADDR(newpath),0,0);
869         user_memdup_free(p,oldpath);
870         user_memdup_free(p,newpath);
871         return ret;
872 }
873
874 intreg_t sys_unlink(struct proc* p, const char* path)
875 {
876         char* fn = user_strdup_errno(p,path,PGSIZE);
877         if(fn == NULL)
878                 return -1;
879         int ret = ufe(unlink,PADDR(fn),0,0,0);
880         user_memdup_free(p,fn);
881         return ret;
882 }
883
884 intreg_t sys_chdir(struct proc* p, const char* path)
885 {
886         char* fn = user_strdup_errno(p,path,PGSIZE);
887         if(fn == NULL)
888                 return -1;
889         int ret = ufe(chdir,PADDR(fn),0,0,0);
890         user_memdup_free(p,fn);
891         return ret;
892 }
893
894 intreg_t sys_getcwd(struct proc* p, char* pwd, int size)
895 {
896         void* kbuf = kmalloc_errno(size);
897         if(kbuf == NULL)
898                 return -1;
899         int ret = ufe(read,PADDR(kbuf),size,0,0);
900         if(ret != -1 && memcpy_to_user_errno(p,pwd,kbuf,strnlen(kbuf,size)))
901                 ret = -1;
902         user_memdup_free(p,kbuf);
903         return ret;
904 }
905
906 intreg_t sys_gettimeofday(struct proc* p, int* buf)
907 {
908         static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
909         static int t0 = 0;
910
911         spin_lock(&gtod_lock);
912         if(t0 == 0)
913                 t0 = ufe(time,0,0,0,0);
914         spin_unlock(&gtod_lock);
915
916         long long dt = read_tsc();
917         int kbuf[2] = {t0+dt/system_timing.tsc_freq,
918             (dt%system_timing.tsc_freq)*1000000/system_timing.tsc_freq};
919
920         return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
921 }
922
923 #define SIZEOF_STRUCT_TERMIOS 60
924 intreg_t sys_tcgetattr(struct proc* p, int fd, void* termios_p)
925 {
926         int* kbuf = kmalloc(SIZEOF_STRUCT_TERMIOS,0);
927         int ret = ufe(tcgetattr,fd,PADDR(kbuf),0,0);
928         if(ret != -1 && memcpy_to_user_errno(p,termios_p,kbuf,SIZEOF_STRUCT_TERMIOS))
929                 ret = -1;
930         kfree(kbuf);
931         return ret;
932 }
933
934 intreg_t sys_tcsetattr(struct proc* p, int fd, int optional_actions, const void* termios_p)
935 {
936         void* kbuf = user_memdup_errno(p,termios_p,SIZEOF_STRUCT_TERMIOS);
937         if(kbuf == NULL)
938                 return -1;
939         int ret = ufe(tcsetattr,fd,optional_actions,PADDR(kbuf),0);
940         user_memdup_free(p,kbuf);
941         return ret;
942 }
943 /************** Syscall Invokation **************/
944
945 /* Executes the given syscall.
946  *
947  * Note tf is passed in, which points to the tf of the context on the kernel
948  * stack.  If any syscall needs to block, it needs to save this info, as well as
949  * any silly state.
950  *
951  * TODO: Build a dispatch table instead of switching on the syscallno
952  * Dispatches to the correct kernel function, passing the arguments.
953  */
954 intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
955                  uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
956 {
957         typedef intreg_t (*syscall_t)(struct proc*,uintreg_t,uintreg_t,
958                                       uintreg_t,uintreg_t,uintreg_t);
959
960         const static syscall_t syscall_table[] = {
961                 [SYS_null] = (syscall_t)sys_null,
962                 [SYS_cache_buster] = (syscall_t)sys_cache_buster,
963                 [SYS_cache_invalidate] = (syscall_t)sys_cache_invalidate,
964                 [SYS_reboot] = (syscall_t)reboot,
965                 [SYS_cputs] = (syscall_t)sys_cputs,
966                 [SYS_cgetc] = (syscall_t)sys_cgetc,
967                 [SYS_getcpuid] = (syscall_t)sys_getcpuid,
968                 [SYS_getvcoreid] = (syscall_t)sys_getvcoreid,
969                 [SYS_getpid] = (syscall_t)sys_getpid,
970                 [SYS_proc_create] = (syscall_t)sys_proc_create,
971                 [SYS_proc_run] = (syscall_t)sys_proc_run,
972                 [SYS_proc_destroy] = (syscall_t)sys_proc_destroy,
973                 [SYS_yield] = (syscall_t)sys_proc_yield,
974                 [SYS_run_binary] = (syscall_t)sys_run_binary,
975                 [SYS_fork] = (syscall_t)sys_fork,
976                 [SYS_exec] = (syscall_t)sys_exec,
977                 [SYS_trywait] = (syscall_t)sys_trywait,
978                 [SYS_mmap] = (syscall_t)sys_mmap,
979                 [SYS_munmap] = (syscall_t)sys_munmap,
980                 [SYS_mprotect] = (syscall_t)sys_mprotect,
981                 [SYS_brk] = (syscall_t)sys_brk,
982                 [SYS_shared_page_alloc] = (syscall_t)sys_shared_page_alloc,
983                 [SYS_shared_page_free] = (syscall_t)sys_shared_page_free,
984                 [SYS_resource_req] = (syscall_t)resource_req,
985         #ifdef __i386__
986                 [SYS_serial_read] = (syscall_t)sys_serial_read,
987                 [SYS_serial_write] = (syscall_t)sys_serial_write,
988         #endif
989         #ifdef __NETWORK__
990                 [SYS_eth_read] = (syscall_t)sys_eth_read,
991                 [SYS_eth_write] = (syscall_t)sys_eth_write,
992                 [SYS_eth_get_mac_addr] = (syscall_t)sys_eth_get_mac_addr,
993                 [SYS_eth_recv_check] = (syscall_t)sys_eth_recv_check,
994         #endif
995                 // Syscalls serviced by the appserver for now.
996                 [SYS_frontend] = (syscall_t)sys_frontend,
997                 [SYS_read] = (syscall_t)sys_read,
998                 [SYS_write] = (syscall_t)sys_write,
999                 [SYS_open] = (syscall_t)sys_open,
1000                 [SYS_close] = (syscall_t)sys_close,
1001                 [SYS_fstat] = (syscall_t)sys_fstat,
1002                 [SYS_stat] = (syscall_t)sys_stat,
1003                 [SYS_lstat] = (syscall_t)sys_lstat,
1004                 [SYS_fcntl] = (syscall_t)sys_fcntl,
1005                 [SYS_access] = (syscall_t)sys_access,
1006                 [SYS_umask] = (syscall_t)sys_umask,
1007                 [SYS_chmod] = (syscall_t)sys_chmod,
1008                 [SYS_lseek] = (syscall_t)sys_lseek,
1009                 [SYS_link] = (syscall_t)sys_link,
1010                 [SYS_unlink] = (syscall_t)sys_unlink,
1011                 [SYS_chdir] = (syscall_t)sys_chdir,
1012                 [SYS_getcwd] = (syscall_t)sys_getcwd,
1013                 [SYS_gettimeofday] = (syscall_t)sys_gettimeofday,
1014                 [SYS_tcgetattr] = (syscall_t)sys_tcgetattr,
1015                 [SYS_tcsetattr] = (syscall_t)sys_tcsetattr
1016         };
1017
1018         const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
1019
1020         //printk("Incoming syscall on core: %d number: %d\n    a1: %x\n   "
1021         //       " a2: %x\n    a3: %x\n    a4: %x\n    a5: %x\n", core_id(),
1022         //       syscallno, a1, a2, a3, a4, a5);
1023
1024         if(syscallno > max_syscall || syscall_table[syscallno] == NULL)
1025                 panic("Invalid syscall number %d for proc %x!", syscallno, *p);
1026
1027         return syscall_table[syscallno](p,a1,a2,a3,a4,a5);
1028 }
1029
1030 intreg_t syscall_async(struct proc *p, syscall_req_t *call)
1031 {
1032         return syscall(p, call->num, call->args[0], call->args[1],
1033                        call->args[2], call->args[3], call->args[4]);
1034 }
1035
1036 /* You should already have a refcnt'd ref to p before calling this */
1037 intreg_t process_generic_syscalls(struct proc *p, size_t max)
1038 {
1039         size_t count = 0;
1040         syscall_back_ring_t* sysbr = &p->syscallbackring;
1041
1042         /* make sure the proc is still alive, and keep it from dying from under us
1043          * incref will return ESUCCESS on success.  This might need some thought
1044          * regarding when the incref should have happened (like by whoever passed us
1045          * the *p). */
1046         // TODO: ought to be unnecessary, if you called this right, kept here for
1047         // now in case anyone actually uses the ARSCs.
1048         proc_incref(p, 1);
1049
1050         // max is the most we'll process.  max = 0 means do as many as possible
1051         while (RING_HAS_UNCONSUMED_REQUESTS(sysbr) && ((!max)||(count < max)) ) {
1052                 if (!count) {
1053                         // ASSUME: one queue per process
1054                         // only switch cr3 for the very first request for this queue
1055                         // need to switch to the right context, so we can handle the user pointer
1056                         // that points to a data payload of the syscall
1057                         lcr3(p->env_cr3);
1058                 }
1059                 count++;
1060                 //printk("DEBUG PRE: sring->req_prod: %d, sring->rsp_prod: %d\n",
1061                 //         sysbr->sring->req_prod, sysbr->sring->rsp_prod);
1062                 // might want to think about 0-ing this out, if we aren't
1063                 // going to explicitly fill in all fields
1064                 syscall_rsp_t rsp;
1065                 // this assumes we get our answer immediately for the syscall.
1066                 syscall_req_t* req = RING_GET_REQUEST(sysbr, ++(sysbr->req_cons));
1067                 rsp.retval = syscall_async(p, req);
1068                 // write response into the slot it came from
1069                 memcpy(req, &rsp, sizeof(syscall_rsp_t));
1070                 // update our counter for what we've produced (assumes we went in order!)
1071                 (sysbr->rsp_prod_pvt)++;
1072                 RING_PUSH_RESPONSES(sysbr);
1073                 //printk("DEBUG POST: sring->req_prod: %d, sring->rsp_prod: %d\n",
1074                 //         sysbr->sring->req_prod, sysbr->sring->rsp_prod);
1075         }
1076         // load sane page tables (and don't rely on decref to do it for you).
1077         lcr3(boot_cr3);
1078         proc_decref(p, 1);
1079         return (intreg_t)count;
1080 }
1081