Prints stderr to the kernel monitor
[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 <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 __CONFIG_NETWORKING__
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 and
164 // this is removed from the user interface
165 static size_t sys_getvcoreid(struct proc *p)
166 {
167         return proc_get_vcoreid(p, core_id());
168 }
169
170 /************** Process management syscalls **************/
171
172 /* Returns the calling process's pid */
173 static pid_t sys_getpid(struct proc *p)
174 {
175         return p->pid;
176 }
177
178 /*
179  * Creates a process found at the user string 'path'.  Currently uses KFS.
180  * Not runnable by default, so it needs it's status to be changed so that the
181  * next call to schedule() will try to run it.
182  * TODO: once we have a decent VFS, consider splitting this up
183  * and once there's an mmap, can have most of this in process.c
184  */
185 static int sys_proc_create(struct proc *p, const char *DANGEROUS path)
186 {
187         int pid = 0;
188         char tpath[MAX_PATH_LEN];
189         /*
190          * There's a bunch of issues with reading in the path, which we'll
191          * need to sort properly in the VFS.  Main concerns are TOCTOU (copy-in),
192          * whether or not it's a big deal that the pointer could be into kernel
193          * space, and resolving both of these without knowing the length of the
194          * string. (TODO)
195          * Change this so that all syscalls with a pointer take a length.
196          *
197          * zra: I've added this user_mem_strlcpy, which I think eliminates the
198      * the TOCTOU issue. Adding a length arg to this call would allow a more
199          * efficient implementation, though, since only one call to user_mem_check
200          * would be required.
201          */
202         int ret = user_mem_strlcpy(p,tpath, path, MAX_PATH_LEN, PTE_USER_RO);
203         int kfs_inode = kfs_lookup_path(tpath);
204         if (kfs_inode < 0)
205                 return -EINVAL;
206         struct proc *new_p = kfs_proc_create(kfs_inode);
207         pid = new_p->pid;
208         proc_decref(new_p, 1); // let go of the reference created in proc_create()
209         return pid;
210 }
211
212 /* Makes process PID runnable.  Consider moving the functionality to process.c */
213 static error_t sys_proc_run(struct proc *p, unsigned pid)
214 {
215         struct proc *target = pid2proc(pid);
216         error_t retval = 0;
217
218         if (!target)
219                 return -EBADPROC;
220         // note we can get interrupted here. it's not bad.
221         spin_lock_irqsave(&p->proc_lock);
222         // make sure we have access and it's in the right state to be activated
223         if (!proc_controls(p, target)) {
224                 proc_decref(target, 1);
225                 retval = -EPERM;
226         } else if (target->state != PROC_CREATED) {
227                 proc_decref(target, 1);
228                 retval = -EINVAL;
229         } else {
230                 __proc_set_state(target, PROC_RUNNABLE_S);
231                 schedule_proc(target);
232         }
233         spin_unlock_irqsave(&p->proc_lock);
234         proc_decref(target, 1);
235         return retval;
236 }
237
238 /* Destroy proc pid.  If this is called by the dying process, it will never
239  * return.  o/w it will return 0 on success, or an error.  Errors include:
240  * - EBADPROC: if there is no such process with pid
241  * - EPERM: if caller does not control pid */
242 static error_t sys_proc_destroy(struct proc *p, pid_t pid, int exitcode)
243 {
244         error_t r;
245         struct proc *p_to_die = pid2proc(pid);
246
247         if (!p_to_die) {
248                 set_errno(current_tf, ESRCH);
249                 return -1;
250         }
251         if (!proc_controls(p, p_to_die)) {
252                 proc_decref(p_to_die, 1);
253                 set_errno(current_tf, EPERM);
254                 return -1;
255         }
256         if (p_to_die == p) {
257                 // syscall code and pid2proc both have edible references, only need 1.
258                 p->exitcode = exitcode;
259                 proc_decref(p, 1);
260                 printd("[PID %d] proc exiting gracefully (code %d)\n", p->pid,exitcode);
261         } else {
262                 panic("Destroying other processes is not supported yet.");
263                 //printk("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
264         }
265         proc_destroy(p_to_die);
266         return ESUCCESS;
267 }
268
269 static int sys_proc_yield(struct proc *p)
270 {
271         proc_yield(p);
272         return 0;
273 }
274
275 static ssize_t sys_run_binary(env_t* e, void *DANGEROUS binary_buf, size_t len,
276                               procinfo_t*DANGEROUS procinfo, size_t num_colors)
277 {
278         env_t* env = proc_create(NULL,0);
279         assert(env != NULL);
280
281         // let me know if you use this.  we need to sort process creation better.
282         printk("sys_run_binary() is deprecated.  Use at your own risk.");
283         if(memcpy_from_user(e,e->procinfo,procinfo,sizeof(*procinfo)))
284                 return -1;
285         proc_init_procinfo(e);
286
287         env_load_icode(env,e,binary_buf,len);
288         __proc_set_state(env, PROC_RUNNABLE_S);
289         schedule_proc(env);
290         if(num_colors > 0) {
291                 env->cache_colors_map = cache_colors_map_alloc();
292                 for(int i=0; i<num_colors; i++)
293                         cache_color_alloc(llc_cache, env->cache_colors_map);
294         }
295         proc_decref(env, 1);
296         proc_yield(e);
297         return 0;
298 }
299
300 static ssize_t sys_fork(env_t* e)
301 {
302         // TODO: right now we only support fork for single-core processes
303         if(e->state != PROC_RUNNING_S)
304         {
305                 set_errno(current_tf,EINVAL);
306                 return -1;
307         }
308
309         env_t* env = proc_create(NULL,0);
310         assert(env != NULL);
311
312         env->heap_top = e->heap_top;
313         env->ppid = e->pid;
314         env->env_tf = *current_tf;
315
316         env->cache_colors_map = cache_colors_map_alloc();
317         for(int i=0; i < llc_cache->num_colors; i++)
318                 if(GET_BITMASK_BIT(e->cache_colors_map,i))
319                         cache_color_alloc(llc_cache, env->cache_colors_map);
320
321         int copy_page(env_t* e, pte_t* pte, void* va, void* arg)
322         {
323                 env_t* env = (env_t*)arg;
324
325                 if(PAGE_PRESENT(*pte))
326                 {
327                         page_t* pp;
328                         if(upage_alloc(env,&pp,0))
329                                 return -1;
330                         if(page_insert(env->env_pgdir,pp,va,*pte & PTE_PERM))
331                         {
332                                 page_decref(pp);
333                                 return -1;
334                         }
335
336                         pagecopy(page2kva(pp),ppn2kva(PTE2PPN(*pte)));
337                 }
338                 else // PAGE_PAGED_OUT(*pte)
339                 {
340                         pte_t* newpte = pgdir_walk(env->env_pgdir,va,1);
341                         if(!newpte)
342                                 return -1;
343
344                         struct file* file = PTE2PFAULT_INFO(*pte)->file;
345                         pfault_info_t* newpfi = pfault_info_alloc(file);
346                         if(!newpfi)
347                                 return -1;
348
349                         *newpfi = *PTE2PFAULT_INFO(*pte);
350                         *newpte = PFAULT_INFO2PTE(newpfi);
351                 }
352
353                 return 0;
354         }
355
356         // TODO: (PC) this won't work.  Needs revisiting.
357         // copy procdata and procinfo
358         memcpy(env->procdata,e->procdata,sizeof(struct procdata));
359         memcpy(env->procinfo,e->procinfo,sizeof(struct procinfo));
360         env->procinfo->pid = env->pid;
361         env->procinfo->ppid = env->ppid;
362
363         // copy all memory below procdata
364         if(env_user_mem_walk(e,0,UDATA,&copy_page,env))
365         {
366                 proc_decref(env,2);
367                 set_errno(current_tf,ENOMEM);
368                 return -1;
369         }
370
371         __proc_set_state(env, PROC_RUNNABLE_S);
372         schedule_proc(env);
373
374         // don't decref the new process.
375         // that will happen when the parent waits for it.
376
377         printd("[PID %d] fork PID %d\n",e->pid,env->pid);
378
379         return env->pid;
380 }
381
382 intreg_t sys_exec(struct proc* p, int fd, procinfo_t* pi)
383 {
384         if(p->state != PROC_RUNNING_S)
385                 return -1;
386
387         int ret = -1;
388         struct file* f = file_open_from_fd(p,fd);
389         if(f == NULL) {
390                 set_errno(current_tf, EBADF);
391                 goto out;
392         }
393
394         // TODO: don't copy procinfo from the user (PC)
395         if(memcpy_from_user(p,p->procinfo,pi,sizeof(procinfo_t))) {
396                 proc_destroy(p);
397                 goto out;
398         }
399         proc_init_procinfo(p);
400         // TODO: don't do this either (PC)
401         memset(p->procdata, 0, sizeof(procdata_t));
402
403         env_user_mem_free(p,0,USTACKTOP);
404
405         if(load_elf(p,f))
406         {
407                 proc_destroy(p);
408                 goto out;
409         }
410         file_decref(f);
411         *current_tf = p->env_tf;
412         ret = 0;
413
414         printd("[PID %d] exec fd %d\n",p->pid,fd);
415
416 out:
417         return ret;
418 }
419
420 static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
421 {
422         struct proc* p = pid2proc(pid);
423
424         // TODO: this syscall is racy, so we only support for single-core procs
425         if(e->state != PROC_RUNNING_S)
426                 return -1;
427
428         // TODO: need to use errno properly.  sadly, ROS error codes conflict..
429
430         if(p)
431         {
432                 ssize_t ret;
433
434                 if(current->pid == p->ppid)
435                 {
436                         if(p->state == PROC_DYING)
437                         {
438                                 memcpy_to_user(e,status,&p->exitcode,sizeof(int));
439                                 printd("[PID %d] waited for PID %d (code %d)\n",
440                                        e->pid,p->pid,p->exitcode);
441                                 ret = 0;
442                         }
443                         else // not dead yet
444                         {
445                                 set_errno(current_tf,0);
446                                 ret = -1;
447                         }
448                 }
449                 else // not a child of the calling process
450                 {
451                         set_errno(current_tf,1);
452                         ret = -1;
453                 }
454
455                 // if the wait succeeded, decref twice
456                 proc_decref(p,1 + (ret == 0));
457                 return ret;
458         }
459
460         set_errno(current_tf,1);
461         return -1;
462 }
463
464 /************** Memory Management Syscalls **************/
465
466 static void *sys_mmap(struct proc* p, uintreg_t a1, uintreg_t a2, uintreg_t a3,
467                       uintreg_t* a456)
468 {
469         uintreg_t _a456[3];
470         if(memcpy_from_user(p,_a456,a456,3*sizeof(uintreg_t)))
471                 sys_proc_destroy(p,p->pid,-1);
472         return mmap(p,a1,a2,a3,_a456[0],_a456[1],_a456[2]);
473 }
474
475 static intreg_t sys_mprotect(struct proc* p, void* addr, size_t len, int prot)
476 {
477         return mprotect(p, addr, len, prot);
478 }
479
480 static intreg_t sys_munmap(struct proc* p, void* addr, size_t len)
481 {
482         return munmap(p, addr, len);
483 }
484
485 static void* sys_brk(struct proc *p, void* addr) {
486         ssize_t range;
487
488         spin_lock_irqsave(&p->proc_lock);
489
490         if((addr < p->procinfo->heap_bottom) || (addr >= (void*)BRK_END))
491                 goto out;
492
493         uintptr_t real_heap_top = ROUNDUP((uintptr_t)p->heap_top,PGSIZE);
494         uintptr_t real_new_heap_top = ROUNDUP((uintptr_t)addr,PGSIZE);
495         range = real_new_heap_top - real_heap_top;
496
497         if (range > 0) {
498                 if(__do_mmap(p, real_heap_top, range, PROT_READ | PROT_WRITE,
499                              MAP_FIXED | MAP_ANONYMOUS, NULL, 0) == MAP_FAILED)
500                         goto out;
501         }
502         else if (range < 0) {
503                 if(__munmap(p, (void*)real_new_heap_top, -range))
504                         goto out;
505         }
506         p->heap_top = addr;
507
508 out:
509         spin_unlock_irqsave(&p->proc_lock);
510         return p->heap_top;
511 }
512
513 static ssize_t sys_shared_page_alloc(env_t* p1,
514                                      void**DANGEROUS _addr, pid_t p2_id,
515                                      int p1_flags, int p2_flags
516                                     )
517 {
518         //if (!VALID_USER_PERMS(p1_flags)) return -EPERM;
519         //if (!VALID_USER_PERMS(p2_flags)) return -EPERM;
520
521         void * COUNT(1) * COUNT(1) addr = user_mem_assert(p1, _addr, sizeof(void *),
522                                                       PTE_USER_RW);
523         struct proc *p2 = pid2proc(p2_id);
524         if (!p2)
525                 return -EBADPROC;
526
527         page_t* page;
528         error_t e = upage_alloc(p1, &page,1);
529         if (e < 0) {
530                 proc_decref(p2, 1);
531                 return e;
532         }
533
534         void* p2_addr = page_insert_in_range(p2->env_pgdir, page,
535                         (void*SNT)UTEXT, (void*SNT)UTOP, p2_flags);
536         if (p2_addr == NULL) {
537                 page_free(page);
538                 proc_decref(p2, 1);
539                 return -EFAIL;
540         }
541
542         void* p1_addr = page_insert_in_range(p1->env_pgdir, page,
543                         (void*SNT)UTEXT, (void*SNT)UTOP, p1_flags);
544         if(p1_addr == NULL) {
545                 page_remove(p2->env_pgdir, p2_addr);
546                 page_free(page);
547                 proc_decref(p2, 1);
548                 return -EFAIL;
549         }
550         *addr = p1_addr;
551         proc_decref(p2, 1);
552         return ESUCCESS;
553 }
554
555 static int sys_shared_page_free(env_t* p1, void*DANGEROUS addr, pid_t p2)
556 {
557         return -1;
558 }
559
560
561 /************** Resource Request Syscalls **************/
562
563 /* sys_resource_req(): called directly from dispatch table. */
564
565 /************** Platform Specific Syscalls **************/
566
567 //Read a buffer over the serial port
568 static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
569 {
570         if (len == 0)
571                 return 0;
572
573         #ifdef __CONFIG_SERIAL_IO__
574             char *COUNT(len) buf = user_mem_assert(e, _buf, len, PTE_USER_RO);
575                 size_t bytes_read = 0;
576                 int c;
577                 while((c = serial_read_byte()) != -1) {
578                         buf[bytes_read++] = (uint8_t)c;
579                         if(bytes_read == len) break;
580                 }
581                 return (ssize_t)bytes_read;
582         #else
583                 return -EINVAL;
584         #endif
585 }
586
587 //Write a buffer over the serial port
588 static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
589 {
590         if (len == 0)
591                 return 0;
592         #ifdef __CONFIG_SERIAL_IO__
593                 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_USER_RO);
594                 for(int i =0; i<len; i++)
595                         serial_send_byte(buf[i]);
596                 return (ssize_t)len;
597         #else
598                 return -EINVAL;
599         #endif
600 }
601
602 #ifdef __CONFIG_NETWORKING__
603 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
604 static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
605 {
606         if (eth_up) {
607
608                 uint32_t len;
609                 char *ptr;
610
611                 spin_lock(&packet_buffers_lock);
612
613                 if (num_packet_buffers == 0) {
614                         spin_unlock(&packet_buffers_lock);
615                         return 0;
616                 }
617
618                 ptr = packet_buffers[packet_buffers_head];
619                 len = packet_buffers_sizes[packet_buffers_head];
620
621                 num_packet_buffers--;
622                 packet_buffers_head = (packet_buffers_head + 1) % MAX_PACKET_BUFFERS;
623
624                 spin_unlock(&packet_buffers_lock);
625
626                 char* _buf = user_mem_assert(e, buf, len, PTE_U);
627
628                 memcpy(_buf, ptr, len);
629
630                 kfree(ptr);
631
632                 return len;
633         }
634         else
635                 return -EINVAL;
636 }
637
638 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
639 static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
640 {
641         if (eth_up) {
642
643                 if (len == 0)
644                         return 0;
645
646                 // HACK TO BYPASS HACK
647                 int just_sent = send_frame(buf, len);
648
649                 if (just_sent < 0) {
650                         printk("Packet send fail\n");
651                         return 0;
652                 }
653
654                 return just_sent;
655
656                 // END OF RECURSIVE HACK
657 /*
658                 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_U);
659                 int total_sent = 0;
660                 int just_sent = 0;
661                 int cur_packet_len = 0;
662                 while (total_sent != len) {
663                         cur_packet_len = ((len - total_sent) > MTU) ? MTU : (len - total_sent);
664                         char dest_mac[6] = APPSERVER_MAC_ADDRESS;
665                         char* wrap_buffer = eth_wrap(_buf + total_sent, cur_packet_len, device_mac, dest_mac, APPSERVER_PORT);
666                         just_sent = send_frame(wrap_buffer, cur_packet_len + sizeof(struct ETH_Header));
667
668                         if (just_sent < 0)
669                                 return 0; // This should be an error code of its own
670
671                         if (wrap_buffer)
672                                 kfree(wrap_buffer);
673
674                         total_sent += cur_packet_len;
675                 }
676
677                 return (ssize_t)len;
678 */
679         }
680         else
681                 return -EINVAL;
682 }
683
684 static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf) 
685 {
686         if (eth_up) {
687                 for (int i = 0; i < 6; i++)
688                         buf[i] = device_mac[i];
689                 return 0;
690         }
691         else
692                 return -EINVAL;
693 }
694
695 static int sys_eth_recv_check(env_t* e) 
696 {
697         if (num_packet_buffers != 0) 
698                 return 1;
699         else
700                 return 0;
701 }
702
703 #endif // Network
704
705 // Syscalls below here are serviced by the appserver for now.
706 #define ufe(which,a0,a1,a2,a3) \
707         frontend_syscall_errno(p,APPSERVER_SYSCALL_##which,\
708                            (int)(a0),(int)(a1),(int)(a2),(int)(a3))
709
710 intreg_t sys_write(struct proc* p, int fd, const void* buf, int len)
711 {
712         int ret = 0;
713         void* kbuf = user_memdup_errno(p,buf,len);
714         if(kbuf == NULL)
715                 return -1;
716 #ifndef __CONFIG_APPSERVER__
717         /* Catch a common usage of stderr */
718         if (fd == 2) {
719                 ((char*)kbuf)[len-1] = 0;
720                 printk("[stderr]: %s\n", kbuf);
721                 ret = len;
722         } else { // but warn/panic otherwise in ufe()
723                 ret = ufe(write, fd, PADDR(kbuf), len, 0);
724         }
725 #else
726         ret = ufe(write, fd, PADDR(kbuf), len, 0);
727 #endif
728         user_memdup_free(p,kbuf);
729         return ret;
730 }
731
732 intreg_t sys_read(struct proc* p, int fd, void* buf, int len)
733 {
734         void* kbuf = kmalloc_errno(len);
735         if(kbuf == NULL)
736                 return -1;
737         int ret = ufe(read,fd,PADDR(kbuf),len,0);
738         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,len))
739                 ret = -1;
740         user_memdup_free(p,kbuf);
741         return ret;
742 }
743
744 intreg_t sys_pwrite(struct proc* p, int fd, const void* buf, int len, int offset)
745 {
746         void* kbuf = user_memdup_errno(p,buf,len);
747         if(kbuf == NULL)
748                 return -1;
749         int ret = ufe(pwrite,fd,PADDR(kbuf),len,offset);
750         user_memdup_free(p,kbuf);
751         return ret;
752 }
753
754 intreg_t sys_pread(struct proc* p, int fd, void* buf, int len, int offset)
755 {
756         void* kbuf = kmalloc_errno(len);
757         if(kbuf == NULL)
758                 return -1;
759         int ret = ufe(pread,fd,PADDR(kbuf),len,offset);
760         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,len))
761                 ret = -1;
762         user_memdup_free(p,kbuf);
763         return ret;
764 }
765
766 intreg_t sys_open(struct proc* p, const char* path, int oflag, int mode)
767 {
768         char* fn = user_strdup_errno(p,path,PGSIZE);
769         if(fn == NULL)
770                 return -1;
771         int ret = ufe(open,PADDR(fn),oflag,mode,0);
772         user_memdup_free(p,fn);
773         return ret;
774 }
775 intreg_t sys_close(struct proc* p, int fd)
776 {
777         return ufe(close,fd,0,0,0);
778 }
779
780 #define NEWLIB_STAT_SIZE 64
781 intreg_t sys_fstat(struct proc* p, int fd, void* buf)
782 {
783         int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
784         int ret = ufe(fstat,fd,PADDR(kbuf),0,0);
785         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
786                 ret = -1;
787         kfree(kbuf);
788         return ret;
789 }
790
791 intreg_t sys_stat(struct proc* p, const char* path, void* buf)
792 {
793         char* fn = user_strdup_errno(p,path,PGSIZE);
794         if(fn == NULL)
795                 return -1;
796
797         int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
798         int ret = ufe(stat,PADDR(fn),PADDR(kbuf),0,0);
799         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
800                 ret = -1;
801
802         user_memdup_free(p,fn);
803         kfree(kbuf);
804         return ret;
805 }
806
807 intreg_t sys_lstat(struct proc* p, const char* path, void* buf)
808 {
809         char* fn = user_strdup_errno(p,path,PGSIZE);
810         if(fn == NULL)
811                 return -1;
812
813         int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
814         int ret = ufe(lstat,PADDR(fn),PADDR(kbuf),0,0);
815         if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
816                 ret = -1;
817
818         user_memdup_free(p,fn);
819         kfree(kbuf);
820         return ret;
821 }
822
823 intreg_t sys_fcntl(struct proc* p, int fd, int cmd, int arg)
824 {
825         return ufe(fcntl,fd,cmd,arg,0);
826 }
827
828 intreg_t sys_access(struct proc* p, const char* path, int type)
829 {
830         char* fn = user_strdup_errno(p,path,PGSIZE);
831         if(fn == NULL)
832                 return -1;
833         int ret = ufe(access,PADDR(fn),type,0,0);
834         user_memdup_free(p,fn);
835         return ret;
836 }
837
838 intreg_t sys_umask(struct proc* p, int mask)
839 {
840         return ufe(umask,mask,0,0,0);
841 }
842
843 intreg_t sys_chmod(struct proc* p, const char* path, int mode)
844 {
845         char* fn = user_strdup_errno(p,path,PGSIZE);
846         if(fn == NULL)
847                 return -1;
848         int ret = ufe(chmod,PADDR(fn),mode,0,0);
849         user_memdup_free(p,fn);
850         return ret;
851 }
852
853 intreg_t sys_lseek(struct proc* p, int fd, int offset, int whence)
854 {
855         return ufe(lseek,fd,offset,whence,0);
856 }
857
858 intreg_t sys_link(struct proc* p, const char* _old, const char* _new)
859 {
860         char* oldpath = user_strdup_errno(p,_old,PGSIZE);
861         if(oldpath == NULL)
862                 return -1;
863
864         char* newpath = user_strdup_errno(p,_new,PGSIZE);
865         if(newpath == NULL)
866         {
867                 user_memdup_free(p,oldpath);
868                 return -1;
869         }
870
871         int ret = ufe(link,PADDR(oldpath),PADDR(newpath),0,0);
872         user_memdup_free(p,oldpath);
873         user_memdup_free(p,newpath);
874         return ret;
875 }
876
877 intreg_t sys_unlink(struct proc* p, const char* path)
878 {
879         char* fn = user_strdup_errno(p,path,PGSIZE);
880         if(fn == NULL)
881                 return -1;
882         int ret = ufe(unlink,PADDR(fn),0,0,0);
883         user_memdup_free(p,fn);
884         return ret;
885 }
886
887 intreg_t sys_chdir(struct proc* p, const char* path)
888 {
889         char* fn = user_strdup_errno(p,path,PGSIZE);
890         if(fn == NULL)
891                 return -1;
892         int ret = ufe(chdir,PADDR(fn),0,0,0);
893         user_memdup_free(p,fn);
894         return ret;
895 }
896
897 intreg_t sys_getcwd(struct proc* p, char* pwd, int size)
898 {
899         void* kbuf = kmalloc_errno(size);
900         if(kbuf == NULL)
901                 return -1;
902         int ret = ufe(read,PADDR(kbuf),size,0,0);
903         if(ret != -1 && memcpy_to_user_errno(p,pwd,kbuf,strnlen(kbuf,size)))
904                 ret = -1;
905         user_memdup_free(p,kbuf);
906         return ret;
907 }
908
909 intreg_t sys_gettimeofday(struct proc* p, int* buf)
910 {
911         static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
912         static int t0 = 0;
913
914         spin_lock(&gtod_lock);
915         if(t0 == 0)
916 #ifdef __CONFIG_APPSERVER__
917                 t0 = ufe(time,0,0,0,0);
918 #else
919                 // Nanwan's birthday, bitches!!
920                 t0 = 1242129600;
921 #endif 
922         spin_unlock(&gtod_lock);
923
924         long long dt = read_tsc();
925         int kbuf[2] = {t0+dt/system_timing.tsc_freq,
926             (dt%system_timing.tsc_freq)*1000000/system_timing.tsc_freq};
927
928         return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
929 }
930
931 #define SIZEOF_STRUCT_TERMIOS 60
932 intreg_t sys_tcgetattr(struct proc* p, int fd, void* termios_p)
933 {
934         int* kbuf = kmalloc(SIZEOF_STRUCT_TERMIOS,0);
935         int ret = ufe(tcgetattr,fd,PADDR(kbuf),0,0);
936         if(ret != -1 && memcpy_to_user_errno(p,termios_p,kbuf,SIZEOF_STRUCT_TERMIOS))
937                 ret = -1;
938         kfree(kbuf);
939         return ret;
940 }
941
942 intreg_t sys_tcsetattr(struct proc* p, int fd, int optional_actions, const void* termios_p)
943 {
944         void* kbuf = user_memdup_errno(p,termios_p,SIZEOF_STRUCT_TERMIOS);
945         if(kbuf == NULL)
946                 return -1;
947         int ret = ufe(tcsetattr,fd,optional_actions,PADDR(kbuf),0);
948         user_memdup_free(p,kbuf);
949         return ret;
950 }
951 /************** Syscall Invokation **************/
952
953 /* Executes the given syscall.
954  *
955  * Note tf is passed in, which points to the tf of the context on the kernel
956  * stack.  If any syscall needs to block, it needs to save this info, as well as
957  * any silly state.
958  *
959  * TODO: Build a dispatch table instead of switching on the syscallno
960  * Dispatches to the correct kernel function, passing the arguments.
961  */
962 intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
963                  uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
964 {
965         // Initialize the return value and error code returned to 0
966         proc_set_syscall_retval(current_tf, 0);
967         set_errno(current_tf,0);
968
969         typedef intreg_t (*syscall_t)(struct proc*,uintreg_t,uintreg_t,
970                                       uintreg_t,uintreg_t,uintreg_t);
971
972         const static syscall_t syscall_table[] = {
973                 [SYS_null] = (syscall_t)sys_null,
974                 [SYS_cache_buster] = (syscall_t)sys_cache_buster,
975                 [SYS_cache_invalidate] = (syscall_t)sys_cache_invalidate,
976                 [SYS_reboot] = (syscall_t)reboot,
977                 [SYS_cputs] = (syscall_t)sys_cputs,
978                 [SYS_cgetc] = (syscall_t)sys_cgetc,
979                 [SYS_getcpuid] = (syscall_t)sys_getcpuid,
980                 [SYS_getvcoreid] = (syscall_t)sys_getvcoreid,
981                 [SYS_getpid] = (syscall_t)sys_getpid,
982                 [SYS_proc_create] = (syscall_t)sys_proc_create,
983                 [SYS_proc_run] = (syscall_t)sys_proc_run,
984                 [SYS_proc_destroy] = (syscall_t)sys_proc_destroy,
985                 [SYS_yield] = (syscall_t)sys_proc_yield,
986                 [SYS_run_binary] = (syscall_t)sys_run_binary,
987                 [SYS_fork] = (syscall_t)sys_fork,
988                 [SYS_exec] = (syscall_t)sys_exec,
989                 [SYS_trywait] = (syscall_t)sys_trywait,
990                 [SYS_mmap] = (syscall_t)sys_mmap,
991                 [SYS_munmap] = (syscall_t)sys_munmap,
992                 [SYS_mprotect] = (syscall_t)sys_mprotect,
993                 [SYS_brk] = (syscall_t)sys_brk,
994                 [SYS_shared_page_alloc] = (syscall_t)sys_shared_page_alloc,
995                 [SYS_shared_page_free] = (syscall_t)sys_shared_page_free,
996                 [SYS_resource_req] = (syscall_t)resource_req,
997         #ifdef __CONFIG_SERIAL_IO__
998                 [SYS_serial_read] = (syscall_t)sys_serial_read,
999                 [SYS_serial_write] = (syscall_t)sys_serial_write,
1000         #endif
1001         #ifdef __CONFIG_NETWORKING__
1002                 [SYS_eth_read] = (syscall_t)sys_eth_read,
1003                 [SYS_eth_write] = (syscall_t)sys_eth_write,
1004                 [SYS_eth_get_mac_addr] = (syscall_t)sys_eth_get_mac_addr,
1005                 [SYS_eth_recv_check] = (syscall_t)sys_eth_recv_check,
1006         #endif
1007                 // Syscalls serviced by the appserver for now.
1008                 [SYS_read] = (syscall_t)sys_read,
1009                 [SYS_write] = (syscall_t)sys_write,
1010                 [SYS_open] = (syscall_t)sys_open,
1011                 [SYS_close] = (syscall_t)sys_close,
1012                 [SYS_fstat] = (syscall_t)sys_fstat,
1013                 [SYS_stat] = (syscall_t)sys_stat,
1014                 [SYS_lstat] = (syscall_t)sys_lstat,
1015                 [SYS_fcntl] = (syscall_t)sys_fcntl,
1016                 [SYS_access] = (syscall_t)sys_access,
1017                 [SYS_umask] = (syscall_t)sys_umask,
1018                 [SYS_chmod] = (syscall_t)sys_chmod,
1019                 [SYS_lseek] = (syscall_t)sys_lseek,
1020                 [SYS_link] = (syscall_t)sys_link,
1021                 [SYS_unlink] = (syscall_t)sys_unlink,
1022                 [SYS_chdir] = (syscall_t)sys_chdir,
1023                 [SYS_getcwd] = (syscall_t)sys_getcwd,
1024                 [SYS_gettimeofday] = (syscall_t)sys_gettimeofday,
1025                 [SYS_tcgetattr] = (syscall_t)sys_tcgetattr,
1026                 [SYS_tcsetattr] = (syscall_t)sys_tcsetattr
1027         };
1028
1029         const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
1030
1031         //printk("Incoming syscall on core: %d number: %d\n    a1: %x\n   "
1032         //       " a2: %x\n    a3: %x\n    a4: %x\n    a5: %x\n", core_id(),
1033         //       syscallno, a1, a2, a3, a4, a5);
1034
1035         if(syscallno > max_syscall || syscall_table[syscallno] == NULL)
1036                 panic("Invalid syscall number %d for proc %x!", syscallno, *p);
1037
1038         return syscall_table[syscallno](p,a1,a2,a3,a4,a5);
1039 }
1040
1041 intreg_t syscall_async(struct proc *p, syscall_req_t *call)
1042 {
1043         return syscall(p, call->num, call->args[0], call->args[1],
1044                        call->args[2], call->args[3], call->args[4]);
1045 }
1046
1047 /* You should already have a refcnt'd ref to p before calling this */
1048 intreg_t process_generic_syscalls(struct proc *p, size_t max)
1049 {
1050         size_t count = 0;
1051         syscall_back_ring_t* sysbr = &p->syscallbackring;
1052
1053         /* make sure the proc is still alive, and keep it from dying from under us
1054          * incref will return ESUCCESS on success.  This might need some thought
1055          * regarding when the incref should have happened (like by whoever passed us
1056          * the *p). */
1057         // TODO: ought to be unnecessary, if you called this right, kept here for
1058         // now in case anyone actually uses the ARSCs.
1059         proc_incref(p, 1);
1060
1061         // max is the most we'll process.  max = 0 means do as many as possible
1062         while (RING_HAS_UNCONSUMED_REQUESTS(sysbr) && ((!max)||(count < max)) ) {
1063                 if (!count) {
1064                         // ASSUME: one queue per process
1065                         // only switch cr3 for the very first request for this queue
1066                         // need to switch to the right context, so we can handle the user pointer
1067                         // that points to a data payload of the syscall
1068                         lcr3(p->env_cr3);
1069                 }
1070                 count++;
1071                 //printk("DEBUG PRE: sring->req_prod: %d, sring->rsp_prod: %d\n",
1072                 //         sysbr->sring->req_prod, sysbr->sring->rsp_prod);
1073                 // might want to think about 0-ing this out, if we aren't
1074                 // going to explicitly fill in all fields
1075                 syscall_rsp_t rsp;
1076                 // this assumes we get our answer immediately for the syscall.
1077                 syscall_req_t* req = RING_GET_REQUEST(sysbr, ++(sysbr->req_cons));
1078                 rsp.retval = syscall_async(p, req);
1079                 // write response into the slot it came from
1080                 memcpy(req, &rsp, sizeof(syscall_rsp_t));
1081                 // update our counter for what we've produced (assumes we went in order!)
1082                 (sysbr->rsp_prod_pvt)++;
1083                 RING_PUSH_RESPONSES(sysbr);
1084                 //printk("DEBUG POST: sring->req_prod: %d, sring->rsp_prod: %d\n",
1085                 //         sysbr->sring->req_prod, sysbr->sring->rsp_prod);
1086         }
1087         // load sane page tables (and don't rely on decref to do it for you).
1088         lcr3(boot_cr3);
1089         proc_decref(p, 1);
1090         return (intreg_t)count;
1091 }
1092