1 /* See COPYRIGHT for copyright information. */
7 #include <ros/common.h>
8 #include <ros/notification.h>
9 #include <arch/types.h>
10 #include <arch/arch.h>
12 #include <arch/console.h>
13 #include <ros/timer.h>
30 #include <colored_caches.h>
31 #include <hashtable.h>
32 #include <arch/bitmask.h>
36 #include <arsc_server.h>
39 #ifdef __CONFIG_NETWORKING__
40 #include <arch/nic_common.h>
41 extern int (*send_frame)(const char *CT(len) data, size_t len);
42 extern unsigned char device_mac[6];
46 int systrace_flags = 0;
47 struct systrace_record *systrace_buffer = 0;
48 unsigned int systrace_bufidx = 0;
49 size_t systrace_bufsize = 0;
50 struct proc *systrace_procs[MAX_NUM_TRACED] = {0};
51 spinlock_t systrace_lock = SPINLOCK_INITIALIZER;
53 /* Not enforcing the packing of systrace_procs yet, but don't rely on that */
54 static bool proc_is_traced(struct proc *p)
56 for (int i = 0; i < MAX_NUM_TRACED; i++)
57 if (systrace_procs[i] == p)
62 /* Helper that "finishes" the current async syscall. This should be used when
63 * we are calling a function in a syscall that might not return and won't be
64 * able to use the normal syscall return path, such as proc_yield() and
65 * resource_req(). Call this from within syscall.c (I don't want it global).
67 * It is possible for another user thread to see the syscall being done early -
68 * they just need to be careful with the weird proc management calls (as in,
69 * don't trust an async fork).
71 * *sysc is in user memory, and should be pinned (TODO: UMEM). There may be
72 * issues with unpinning this if we never return. */
73 static void signal_current_sc(int retval)
75 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
76 pcpui->cur_sysc->retval = retval;
77 pcpui->cur_sysc->flags |= SC_DONE;
80 /* Callable by any function while executing a syscall (or otherwise, actually).
82 void set_errno(int errno)
84 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
86 pcpui->cur_sysc->err = errno;
89 /************** Utility Syscalls **************/
91 static int sys_null(void)
96 // Writes 'val' to 'num_writes' entries of the well-known array in the kernel
97 // address space. It's just #defined to be some random 4MB chunk (which ought
98 // to be boot_alloced or something). Meant to grab exclusive access to cache
99 // lines, to simulate doing something useful.
100 static int sys_cache_buster(struct proc *p, uint32_t num_writes,
101 uint32_t num_pages, uint32_t flags)
102 { TRUSTEDBLOCK /* zra: this is not really part of the kernel */
103 #define BUSTER_ADDR 0xd0000000 // around 512 MB deep
104 #define MAX_WRITES 1048576*8
106 #define INSERT_ADDR (UINFO + 2*PGSIZE) // should be free for these tests
107 uint32_t* buster = (uint32_t*)BUSTER_ADDR;
108 static spinlock_t buster_lock = SPINLOCK_INITIALIZER;
110 page_t* a_page[MAX_PAGES];
112 /* Strided Accesses or Not (adjust to step by cachelines) */
114 if (flags & BUSTER_STRIDED) {
119 /* Shared Accesses or Not (adjust to use per-core regions)
120 * Careful, since this gives 8MB to each core, starting around 512MB.
121 * Also, doesn't separate memory for core 0 if it's an async call.
123 if (!(flags & BUSTER_SHARED))
124 buster = (uint32_t*)(BUSTER_ADDR + core_id() * 0x00800000);
126 /* Start the timer, if we're asked to print this info*/
127 if (flags & BUSTER_PRINT_TICKS)
128 ticks = start_timing();
130 /* Allocate num_pages (up to MAX_PAGES), to simulate doing some more
131 * realistic work. Note we don't write to these pages, even if we pick
132 * unshared. Mostly due to the inconvenience of having to match up the
133 * number of pages with the number of writes. And it's unnecessary.
136 spin_lock(&buster_lock);
137 for (int i = 0; i < MIN(num_pages, MAX_PAGES); i++) {
138 upage_alloc(p, &a_page[i],1);
139 page_insert(p->env_pgdir, a_page[i], (void*)INSERT_ADDR + PGSIZE*i,
141 page_decref(a_page[i]);
143 spin_unlock(&buster_lock);
146 if (flags & BUSTER_LOCKED)
147 spin_lock(&buster_lock);
148 for (int i = 0; i < MIN(num_writes, MAX_WRITES); i=i+stride)
149 buster[i] = 0xdeadbeef;
150 if (flags & BUSTER_LOCKED)
151 spin_unlock(&buster_lock);
154 spin_lock(&buster_lock);
155 for (int i = 0; i < MIN(num_pages, MAX_PAGES); i++) {
156 page_remove(p->env_pgdir, (void*)(INSERT_ADDR + PGSIZE * i));
157 page_decref(a_page[i]);
159 spin_unlock(&buster_lock);
163 if (flags & BUSTER_PRINT_TICKS) {
164 ticks = stop_timing(ticks);
165 printk("%llu,", ticks);
170 static int sys_cache_invalidate(void)
178 /* sys_reboot(): called directly from dispatch table. */
180 /* Print a string to the system console. */
181 static ssize_t sys_cputs(struct proc *p, const char *DANGEROUS string,
185 t_string = user_strdup_errno(p, string, strlen);
188 printk("%.*s", strlen, t_string);
189 user_memdup_free(p, t_string);
190 return (ssize_t)strlen;
193 // Read a character from the system console.
194 // Returns the character.
195 static uint16_t sys_cgetc(struct proc *p)
199 // The cons_getc() primitive doesn't wait for a character,
200 // but the sys_cgetc() system call does.
201 while ((c = cons_getc()) == 0)
207 /* Returns the id of the cpu this syscall is executed on. */
208 static uint32_t sys_getcpuid(void)
213 // TODO: Temporary hack until thread-local storage is implemented on i386 and
214 // this is removed from the user interface
215 static size_t sys_getvcoreid(struct proc *p)
217 return proc_get_vcoreid(p, core_id());
220 /************** Process management syscalls **************/
222 /* Returns the calling process's pid */
223 static pid_t sys_getpid(struct proc *p)
228 /* Creates a process from the file 'path'. The process is not runnable by
229 * default, so it needs it's status to be changed so that the next call to
230 * schedule() will try to run it. TODO: take args/envs from userspace. */
231 static int sys_proc_create(struct proc *p, char *path, size_t path_l,
236 struct file *program;
239 /* Copy in the path. Consider putting an upper bound on path_l. */
240 t_path = user_strdup_errno(p, path, path_l);
243 program = do_file_open(t_path, 0, 0);
244 user_memdup_free(p, t_path);
246 return -1; /* presumably, errno is already set */
247 /* TODO: need to split the proc creation, since you must load after setting
248 * args/env, since auxp gets set up there. */
249 //new_p = proc_create(program, 0, 0);
250 if (proc_alloc(&new_p, current))
252 /* Set the argument stuff needed by glibc */
253 if (memcpy_from_user_errno(p, new_p->procinfo->argp, pi->argp,
256 if (memcpy_from_user_errno(p, new_p->procinfo->argbuf, pi->argbuf,
259 if (load_elf(new_p, program))
261 kref_put(&program->f_kref);
262 /* Connect to stdin, stdout, stderr (part of proc_create()) */
263 assert(insert_file(&new_p->open_files, dev_stdin, 0) == 0);
264 assert(insert_file(&new_p->open_files, dev_stdout, 0) == 1);
265 assert(insert_file(&new_p->open_files, dev_stderr, 0) == 2);
268 kref_put(&new_p->kref); /* give up the reference created in proc_create() */
273 kref_put(&program->f_kref);
277 /* Makes process PID runnable. Consider moving the functionality to process.c */
278 static error_t sys_proc_run(struct proc *p, unsigned pid)
280 struct proc *target = pid2proc(pid);
285 // note we can get interrupted here. it's not bad.
286 spin_lock(&p->proc_lock);
287 // make sure we have access and it's in the right state to be activated
288 if (!proc_controls(p, target)) {
289 kref_put(&target->kref);
291 } else if (target->state != PROC_CREATED) {
292 kref_put(&target->kref);
295 __proc_set_state(target, PROC_RUNNABLE_S);
296 schedule_proc(target);
298 spin_unlock(&p->proc_lock);
299 kref_put(&target->kref);
303 /* Destroy proc pid. If this is called by the dying process, it will never
304 * return. o/w it will return 0 on success, or an error. Errors include:
305 * - EBADPROC: if there is no such process with pid
306 * - EPERM: if caller does not control pid */
307 static error_t sys_proc_destroy(struct proc *p, pid_t pid, int exitcode)
310 struct proc *p_to_die = pid2proc(pid);
316 if (!proc_controls(p, p_to_die)) {
317 kref_put(&p_to_die->kref);
322 p->exitcode = exitcode;
323 printd("[PID %d] proc exiting gracefully (code %d)\n", p->pid,exitcode);
325 printd("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
327 proc_destroy(p_to_die);
328 /* we only get here if we weren't the one to die */
329 kref_put(&p_to_die->kref);
333 static int sys_proc_yield(struct proc *p, bool being_nice)
335 /* proc_yield() often doesn't return - we need to set the syscall retval
336 * early. If it doesn't return, it expects to eat our reference (for now).
338 signal_current_sc(0);
339 kref_get(&p->kref, 1);
340 proc_yield(p, being_nice);
345 static ssize_t sys_fork(env_t* e)
347 // TODO: right now we only support fork for single-core processes
348 if (e->state != PROC_RUNNING_S) {
352 /* Can't really fork if we don't have a current_tf to fork */
358 assert(!proc_alloc(&env, current));
361 env->heap_top = e->heap_top;
363 env->env_tf = *current_tf;
365 /* We need to speculatively say the syscall worked before copying the memory
366 * out, since the 'forked' process's call never actually goes through the
367 * syscall return path, and will never think it is done. This violates a
368 * few things. Just be careful with fork. */
369 signal_current_sc(0);
371 env->cache_colors_map = cache_colors_map_alloc();
372 for(int i=0; i < llc_cache->num_colors; i++)
373 if(GET_BITMASK_BIT(e->cache_colors_map,i))
374 cache_color_alloc(llc_cache, env->cache_colors_map);
376 duplicate_vmrs(e, env);
378 int copy_page(env_t* e, pte_t* pte, void* va, void* arg)
380 env_t* env = (env_t*)arg;
382 if(PAGE_PRESENT(*pte))
385 if(upage_alloc(env,&pp,0))
387 if(page_insert(env->env_pgdir,pp,va,*pte & PTE_PERM))
392 pagecopy(page2kva(pp),ppn2kva(PTE2PPN(*pte)));
395 assert(PAGE_PAGED_OUT(*pte));
396 /* TODO: (SWAP) will need to either make a copy or CoW/refcnt the
397 * backend store. For now, this PTE will be the same as the
399 panic("Swapping not supported!");
400 pte_t* newpte = pgdir_walk(env->env_pgdir,va,1);
408 // TODO: (PC) this won't work. Needs revisiting.
409 // copy procdata and procinfo
410 memcpy(env->procdata,e->procdata,sizeof(struct procdata));
411 memcpy(env->procinfo,e->procinfo,sizeof(struct procinfo));
412 env->procinfo->pid = env->pid;
413 env->procinfo->ppid = env->ppid;
415 /* for now, just copy the contents of every present page in the entire
417 if (env_user_mem_walk(e, 0, UMAPTOP, ©_page, env)) {
418 proc_destroy(env); /* this is prob what you want, not decref by 2 */
422 clone_files(&e->open_files, &env->open_files);
424 __proc_set_state(env, PROC_RUNNABLE_S);
427 // don't decref the new process.
428 // that will happen when the parent waits for it.
429 // TODO: if the parent doesn't wait, we need to change the child's parent
430 // when the parent dies, or at least decref it
432 printd("[PID %d] fork PID %d\n",e->pid,env->pid);
436 /* Load the binary "path" into the current process, and start executing it.
437 * argv and envp are magically bundled in procinfo for now. Keep in sync with
438 * glibc's sysdeps/ros/execve.c. Once past a certain point, this function won't
439 * return. It assumes (and checks) that it is current. Don't give it an extra
440 * refcnt'd *p (syscall won't do that).
441 * Note: if someone batched syscalls with this call, they could clobber their
442 * old memory (and will likely PF and die). Don't do it... */
443 static int sys_exec(struct proc *p, char *path, size_t path_l,
448 struct file *program;
449 struct trapframe *old_cur_tf = current_tf;
450 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
452 /* We probably want it to never be allowed to exec if it ever was _M */
453 if (p->state != PROC_RUNNING_S) {
461 /* Can't exec if we don't have a current_tf to restart (if we fail). */
466 /* Copy in the path. Consider putting an upper bound on path_l. */
467 t_path = user_strdup_errno(p, path, path_l);
470 /* Clear the current_tf. We won't be returning the 'normal' way. Even if
471 * we want to return with an error, we need to go back differently in case
472 * we succeed. This needs to be done before we could possibly block, but
473 * unfortunately happens before the point of no return. */
475 /* This could block: */
476 program = do_file_open(t_path, 0, 0);
477 user_memdup_free(p, t_path);
480 /* Set the argument stuff needed by glibc */
481 if (memcpy_from_user_errno(p, p->procinfo->argp, pi->argp,
484 if (memcpy_from_user_errno(p, p->procinfo->argbuf, pi->argbuf,
487 /* This is the point of no return for the process. */
488 /* TODO: issues with this: Need to also assert there are no outstanding
489 * users of the sysrings. the ldt page will get freed shortly, so that's
490 * okay. Potentially issues with the nm and vcpd if we were in _M before
491 * and someone is trying to notify. */
492 memset(p->procdata, 0, sizeof(procdata_t));
494 close_all_files(&p->open_files, TRUE);
495 env_user_mem_free(p, 0, UMAPTOP);
496 if (load_elf(p, program)) {
497 kref_put(&program->f_kref);
499 /* We don't want to do anything else - we just need to not accidentally
500 * return to the user (hence the all_out) */
503 printd("[PID %d] exec %s\n", p->pid, file_name(program));
504 kref_put(&program->f_kref);
506 /* These error and out paths are so we can handle the async interface, both
507 * for when we want to error/return to the proc, as well as when we succeed
508 * and want to start the newly exec'd _S */
510 /* These two error paths are for when we want to restart the process with an
511 * error value (errno is already set). */
512 kref_put(&program->f_kref);
514 p->env_tf = *old_cur_tf;
515 signal_current_sc(-1);
517 /* Here's how we'll restart the new (or old) process: */
518 spin_lock(&p->proc_lock);
519 __proc_set_state(p, PROC_RUNNABLE_S);
521 spin_unlock(&p->proc_lock);
523 /* we can't return, since we'd write retvals to the old location of the
524 * sycall struct (which has been freed and is in the old userspace) (or has
525 * already been written to).*/
531 static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
533 struct proc* p = pid2proc(pid);
535 // TODO: this syscall is racy, so we only support for single-core procs
536 if(e->state != PROC_RUNNING_S)
539 // TODO: need to use errno properly. sadly, ROS error codes conflict..
545 if(current->pid == p->ppid)
547 if(p->state == PROC_DYING)
549 memcpy_to_user(e,status,&p->exitcode,sizeof(int));
550 printd("[PID %d] waited for PID %d (code %d)\n",
551 e->pid,p->pid,p->exitcode);
560 else // not a child of the calling process
566 // if the wait succeeded, decref twice
577 /************** Memory Management Syscalls **************/
579 static void *sys_mmap(struct proc *p, uintptr_t addr, size_t len, int prot,
580 int flags, int fd, off_t offset)
582 return mmap(p, addr, len, prot, flags, fd, offset);
585 static intreg_t sys_mprotect(struct proc *p, void *addr, size_t len, int prot)
587 return mprotect(p, (uintptr_t)addr, len, prot);
590 static intreg_t sys_munmap(struct proc *p, void *addr, size_t len)
592 return munmap(p, (uintptr_t)addr, len);
595 static ssize_t sys_shared_page_alloc(env_t* p1,
596 void**DANGEROUS _addr, pid_t p2_id,
597 int p1_flags, int p2_flags
600 printk("[kernel] shared page alloc is deprecated/unimplemented.\n");
604 static int sys_shared_page_free(env_t* p1, void*DANGEROUS addr, pid_t p2)
610 static int sys_resource_req(struct proc *p, int type, unsigned int amt_wanted,
611 unsigned int amt_wanted_min, int flags)
614 signal_current_sc(0);
615 /* this might not return (if it's a _S -> _M transition) */
616 kref_get(&p->kref, 1);
617 retval = resource_req(p, type, amt_wanted, amt_wanted_min, flags);
622 /* Will notify the target on the given vcore, if the caller controls the target.
623 * Will honor the target's wanted/vcoreid. u_ne can be NULL. */
624 static int sys_notify(struct proc *p, int target_pid, unsigned int notif,
625 struct notif_event *u_ne)
627 struct notif_event local_ne;
628 struct proc *target = pid2proc(target_pid);
634 if (!proc_controls(p, target)) {
635 kref_put(&target->kref);
639 /* if the user provided a notif_event, copy it in and use that */
641 if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
642 kref_put(&target->kref);
646 proc_notify(target, local_ne.ne_type, &local_ne);
648 proc_notify(target, notif, 0);
650 kref_put(&target->kref);
654 /* Will notify the calling process on the given vcore, independently of WANTED
655 * or advertised vcoreid. If you change the parameters, change pop_ros_tf() */
656 static int sys_self_notify(struct proc *p, uint32_t vcoreid, unsigned int notif,
657 struct notif_event *u_ne)
659 struct notif_event local_ne;
661 printd("[kernel] received self notify for vcoreid %d, notif %d, ne %08p\n",
662 vcoreid, notif, u_ne);
663 /* if the user provided a notif_event, copy it in and use that */
665 if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
669 do_notify(p, vcoreid, local_ne.ne_type, &local_ne);
671 do_notify(p, vcoreid, notif, 0);
676 /* This will set a local timer for usec, then shut down the core */
677 static int sys_halt_core(struct proc *p, unsigned int usec)
679 /* TODO: ought to check and see if a timer was already active, etc, esp so
680 * userspace can't turn off timers. also note we will also call whatever
681 * timer_interrupt() will do, though all we care about is just
682 * self_ipi/interrupting. */
683 set_core_timer(usec);
685 set_core_timer(0); /* Disable the timer (we don't have a 0-shot yet) */
690 /************** Platform Specific Syscalls **************/
692 //Read a buffer over the serial port
693 static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
695 printk("[kernel] serial reading is deprecated.\n");
699 #ifdef __CONFIG_SERIAL_IO__
700 char *COUNT(len) buf = user_mem_assert(e, _buf, len, PTE_USER_RO);
701 size_t bytes_read = 0;
703 while((c = serial_read_byte()) != -1) {
704 buf[bytes_read++] = (uint8_t)c;
705 if(bytes_read == len) break;
707 return (ssize_t)bytes_read;
713 //Write a buffer over the serial port
714 static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
716 printk("[kernel] serial writing is deprecated.\n");
719 #ifdef __CONFIG_SERIAL_IO__
720 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_USER_RO);
721 for(int i =0; i<len; i++)
722 serial_send_byte(buf[i]);
729 #ifdef __CONFIG_NETWORKING__
730 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
731 static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
738 spin_lock(&packet_buffers_lock);
740 if (num_packet_buffers == 0) {
741 spin_unlock(&packet_buffers_lock);
745 ptr = packet_buffers[packet_buffers_head];
746 len = packet_buffers_sizes[packet_buffers_head];
748 num_packet_buffers--;
749 packet_buffers_head = (packet_buffers_head + 1) % MAX_PACKET_BUFFERS;
751 spin_unlock(&packet_buffers_lock);
753 char* _buf = user_mem_assert(e, buf, len, PTE_U);
755 memcpy(_buf, ptr, len);
765 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
766 static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
773 // HACK TO BYPASS HACK
774 int just_sent = send_frame(buf, len);
777 printk("Packet send fail\n");
783 // END OF RECURSIVE HACK
785 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_U);
788 int cur_packet_len = 0;
789 while (total_sent != len) {
790 cur_packet_len = ((len - total_sent) > MTU) ? MTU : (len - total_sent);
791 char dest_mac[6] = APPSERVER_MAC_ADDRESS;
792 char* wrap_buffer = eth_wrap(_buf + total_sent, cur_packet_len, device_mac, dest_mac, APPSERVER_PORT);
793 just_sent = send_frame(wrap_buffer, cur_packet_len + sizeof(struct ETH_Header));
796 return 0; // This should be an error code of its own
801 total_sent += cur_packet_len;
811 static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf)
814 for (int i = 0; i < 6; i++)
815 buf[i] = device_mac[i];
822 static int sys_eth_recv_check(env_t* e)
824 if (num_packet_buffers != 0)
832 static intreg_t sys_read(struct proc *p, int fd, void *buf, int len)
835 struct file *file = get_file_from_fd(&p->open_files, fd);
840 if (!file->f_op->read) {
841 kref_put(&file->f_kref);
845 /* TODO: (UMEM) currently, read() handles user memcpy issues, but we
846 * probably should user_mem_check and pin the region here, so read doesn't
848 ret = file->f_op->read(file, buf, len, &file->f_pos);
849 kref_put(&file->f_kref);
853 static intreg_t sys_write(struct proc *p, int fd, const void *buf, int len)
856 struct file *file = get_file_from_fd(&p->open_files, fd);
861 if (!file->f_op->write) {
862 kref_put(&file->f_kref);
867 ret = file->f_op->write(file, buf, len, &file->f_pos);
868 kref_put(&file->f_kref);
872 /* Checks args/reads in the path, opens the file, and inserts it into the
873 * process's open file list.
875 * TODO: take the path length */
876 static intreg_t sys_open(struct proc *p, const char *path, size_t path_l,
882 printd("File %s Open attempt\n", path);
883 char *t_path = user_strdup_errno(p, path, path_l);
886 mode &= ~p->fs_env.umask;
887 file = do_file_open(t_path, oflag, mode);
888 user_memdup_free(p, t_path);
891 fd = insert_file(&p->open_files, file, 0); /* stores the ref to file */
892 kref_put(&file->f_kref);
894 warn("File insertion failed");
897 printd("File %s Open, res=%d\n", path, fd);
901 static intreg_t sys_close(struct proc *p, int fd)
903 struct file *file = put_file_from_fd(&p->open_files, fd);
911 /* kept around til we remove the last ufe */
912 #define ufe(which,a0,a1,a2,a3) \
913 frontend_syscall_errno(p,APPSERVER_SYSCALL_##which,\
914 (int)(a0),(int)(a1),(int)(a2),(int)(a3))
916 static intreg_t sys_fstat(struct proc *p, int fd, struct kstat *u_stat)
919 struct file *file = get_file_from_fd(&p->open_files, fd);
924 kbuf = kmalloc(sizeof(struct kstat), 0);
926 kref_put(&file->f_kref);
930 stat_inode(file->f_dentry->d_inode, kbuf);
931 kref_put(&file->f_kref);
932 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
933 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
942 /* sys_stat() and sys_lstat() do nearly the same thing, differing in how they
943 * treat a symlink for the final item, which (probably) will be controlled by
944 * the lookup flags */
945 static intreg_t stat_helper(struct proc *p, const char *path, size_t path_l,
946 struct kstat *u_stat, int flags)
949 struct dentry *path_d;
950 char *t_path = user_strdup_errno(p, path, path_l);
953 path_d = lookup_dentry(t_path, flags);
954 user_memdup_free(p, t_path);
957 kbuf = kmalloc(sizeof(struct kstat), 0);
960 kref_put(&path_d->d_kref);
963 stat_inode(path_d->d_inode, kbuf);
964 kref_put(&path_d->d_kref);
965 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
966 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
975 /* Follow a final symlink */
976 static intreg_t sys_stat(struct proc *p, const char *path, size_t path_l,
977 struct kstat *u_stat)
979 return stat_helper(p, path, path_l, u_stat, LOOKUP_FOLLOW);
982 /* Don't follow a final symlink */
983 static intreg_t sys_lstat(struct proc *p, const char *path, size_t path_l,
984 struct kstat *u_stat)
986 return stat_helper(p, path, path_l, u_stat, 0);
989 intreg_t sys_fcntl(struct proc *p, int fd, int cmd, int arg)
992 struct file *file = get_file_from_fd(&p->open_files, fd);
999 retval = insert_file(&p->open_files, file, arg);
1006 retval = p->open_files.fd[fd].fd_flags;
1009 if (arg == FD_CLOEXEC)
1010 file->f_flags |= O_CLOEXEC;
1013 retval = file->f_flags;
1016 /* only allowed to set certain flags. */
1017 arg &= O_FCNTL_FLAGS;
1018 file->f_flags = (file->f_flags & ~O_FCNTL_FLAGS) | arg;
1021 warn("Unsupported fcntl cmd %d\n", cmd);
1023 kref_put(&file->f_kref);
1027 static intreg_t sys_access(struct proc *p, const char *path, size_t path_l,
1031 char *t_path = user_strdup_errno(p, path, path_l);
1034 retval = do_access(t_path, mode);
1035 user_memdup_free(p, t_path);
1036 printd("Access for path: %s retval: %d\n", path, retval);
1044 intreg_t sys_umask(struct proc *p, int mask)
1046 int old_mask = p->fs_env.umask;
1047 p->fs_env.umask = mask & S_PMASK;
1051 intreg_t sys_chmod(struct proc *p, const char *path, size_t path_l, int mode)
1054 char *t_path = user_strdup_errno(p, path, path_l);
1057 retval = do_chmod(t_path, mode);
1058 user_memdup_free(p, t_path);
1066 static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
1069 struct file *file = get_file_from_fd(&p->open_files, fd);
1074 ret = file->f_op->llseek(file, offset, whence);
1075 kref_put(&file->f_kref);
1079 intreg_t sys_link(struct proc *p, char *old_path, size_t old_l,
1080 char *new_path, size_t new_l)
1083 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1084 if (t_oldpath == NULL)
1086 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1087 if (t_newpath == NULL) {
1088 user_memdup_free(p, t_oldpath);
1091 ret = do_link(t_oldpath, t_newpath);
1092 user_memdup_free(p, t_oldpath);
1093 user_memdup_free(p, t_newpath);
1097 intreg_t sys_unlink(struct proc *p, const char *path, size_t path_l)
1100 char *t_path = user_strdup_errno(p, path, path_l);
1103 retval = do_unlink(t_path);
1104 user_memdup_free(p, t_path);
1108 intreg_t sys_symlink(struct proc *p, char *old_path, size_t old_l,
1109 char *new_path, size_t new_l)
1112 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1113 if (t_oldpath == NULL)
1115 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1116 if (t_newpath == NULL) {
1117 user_memdup_free(p, t_oldpath);
1120 ret = do_symlink(new_path, old_path, S_IRWXU | S_IRWXG | S_IRWXO);
1121 user_memdup_free(p, t_oldpath);
1122 user_memdup_free(p, t_newpath);
1126 intreg_t sys_readlink(struct proc *p, char *path, size_t path_l,
1127 char *u_buf, size_t buf_l)
1131 struct dentry *path_d;
1132 char *t_path = user_strdup_errno(p, path, path_l);
1135 path_d = lookup_dentry(t_path, 0);
1136 user_memdup_free(p, t_path);
1139 symname = path_d->d_inode->i_op->readlink(path_d);
1140 copy_amt = strnlen(symname, buf_l - 1) + 1;
1141 if (memcpy_to_user_errno(p, u_buf, symname, copy_amt)) {
1142 kref_put(&path_d->d_kref);
1146 kref_put(&path_d->d_kref);
1147 printd("READLINK returning %s\n", u_buf);
1151 intreg_t sys_chdir(struct proc *p, const char *path, size_t path_l)
1154 char *t_path = user_strdup_errno(p, path, path_l);
1157 retval = do_chdir(&p->fs_env, t_path);
1158 user_memdup_free(p, t_path);
1166 /* Note cwd_l is not a strlen, it's an absolute size */
1167 intreg_t sys_getcwd(struct proc *p, char *u_cwd, size_t cwd_l)
1171 char *k_cwd = do_getcwd(&p->fs_env, &kfree_this, cwd_l);
1173 return -1; /* errno set by do_getcwd */
1174 if (memcpy_to_user_errno(p, u_cwd, k_cwd, strnlen(k_cwd, cwd_l - 1) + 1))
1180 intreg_t sys_mkdir(struct proc *p, const char *path, size_t path_l, int mode)
1183 char *t_path = user_strdup_errno(p, path, path_l);
1186 mode &= ~p->fs_env.umask;
1187 retval = do_mkdir(t_path, mode);
1188 user_memdup_free(p, t_path);
1192 intreg_t sys_rmdir(struct proc *p, const char *path, size_t path_l)
1195 char *t_path = user_strdup_errno(p, path, path_l);
1198 retval = do_rmdir(t_path);
1199 user_memdup_free(p, t_path);
1203 intreg_t sys_gettimeofday(struct proc *p, int *buf)
1205 static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
1208 spin_lock(>od_lock);
1211 #if (defined __CONFIG_APPSERVER__)
1212 t0 = ufe(time,0,0,0,0);
1214 // Nanwan's birthday, bitches!!
1217 spin_unlock(>od_lock);
1219 long long dt = read_tsc();
1220 int kbuf[2] = {t0+dt/system_timing.tsc_freq,
1221 (dt%system_timing.tsc_freq)*1000000/system_timing.tsc_freq};
1223 return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
1226 #define SIZEOF_STRUCT_TERMIOS 60
1227 intreg_t sys_tcgetattr(struct proc *p, int fd, void *termios_p)
1229 int* kbuf = kmalloc(SIZEOF_STRUCT_TERMIOS,0);
1230 int ret = ufe(tcgetattr,fd,PADDR(kbuf),0,0);
1231 if(ret != -1 && memcpy_to_user_errno(p,termios_p,kbuf,SIZEOF_STRUCT_TERMIOS))
1237 intreg_t sys_tcsetattr(struct proc *p, int fd, int optional_actions,
1238 const void *termios_p)
1240 void* kbuf = user_memdup_errno(p,termios_p,SIZEOF_STRUCT_TERMIOS);
1243 int ret = ufe(tcsetattr,fd,optional_actions,PADDR(kbuf),0);
1244 user_memdup_free(p,kbuf);
1248 /* TODO: we don't have any notion of UIDs or GIDs yet, but don't let that stop a
1249 * process from thinking it can do these. The other alternative is to have
1250 * glibc return 0 right away, though someone might want to do something with
1251 * these calls. Someday. */
1252 intreg_t sys_setuid(struct proc *p, uid_t uid)
1257 intreg_t sys_setgid(struct proc *p, gid_t gid)
1262 /************** Syscall Invokation **************/
1264 const static struct sys_table_entry syscall_table[] = {
1265 [SYS_null] = {(syscall_t)sys_null, "null"},
1266 [SYS_cache_buster] = {(syscall_t)sys_cache_buster, "buster"},
1267 [SYS_cache_invalidate] = {(syscall_t)sys_cache_invalidate, "wbinv"},
1268 [SYS_reboot] = {(syscall_t)reboot, "reboot!"},
1269 [SYS_cputs] = {(syscall_t)sys_cputs, "cputs"},
1270 [SYS_cgetc] = {(syscall_t)sys_cgetc, "cgetc"},
1271 [SYS_getcpuid] = {(syscall_t)sys_getcpuid, "getcpuid"},
1272 [SYS_getvcoreid] = {(syscall_t)sys_getvcoreid, "getvcoreid"},
1273 [SYS_getpid] = {(syscall_t)sys_getpid, "getpid"},
1274 [SYS_proc_create] = {(syscall_t)sys_proc_create, "proc_create"},
1275 [SYS_proc_run] = {(syscall_t)sys_proc_run, "proc_run"},
1276 [SYS_proc_destroy] = {(syscall_t)sys_proc_destroy, "proc_destroy"},
1277 [SYS_yield] = {(syscall_t)sys_proc_yield, "proc_yield"},
1278 [SYS_fork] = {(syscall_t)sys_fork, "fork"},
1279 [SYS_exec] = {(syscall_t)sys_exec, "exec"},
1280 [SYS_trywait] = {(syscall_t)sys_trywait, "trywait"},
1281 [SYS_mmap] = {(syscall_t)sys_mmap, "mmap"},
1282 [SYS_munmap] = {(syscall_t)sys_munmap, "munmap"},
1283 [SYS_mprotect] = {(syscall_t)sys_mprotect, "mprotect"},
1284 [SYS_shared_page_alloc] = {(syscall_t)sys_shared_page_alloc, "pa"},
1285 [SYS_shared_page_free] = {(syscall_t)sys_shared_page_free, "pf"},
1286 [SYS_resource_req] = {(syscall_t)sys_resource_req, "resource_req"},
1287 [SYS_notify] = {(syscall_t)sys_notify, "notify"},
1288 [SYS_self_notify] = {(syscall_t)sys_self_notify, "self_notify"},
1289 [SYS_halt_core] = {(syscall_t)sys_halt_core, "halt_core"},
1290 #ifdef __CONFIG_SERIAL_IO__
1291 [SYS_serial_read] = {(syscall_t)sys_serial_read, "ser_read"},
1292 [SYS_serial_write] = {(syscall_t)sys_serial_write, "ser_write"},
1294 #ifdef __CONFIG_NETWORKING__
1295 [SYS_eth_read] = {(syscall_t)sys_eth_read, "eth_read"},
1296 [SYS_eth_write] = {(syscall_t)sys_eth_write, "eth_write"},
1297 [SYS_eth_get_mac_addr] = {(syscall_t)sys_eth_get_mac_addr, "get_mac"},
1298 [SYS_eth_recv_check] = {(syscall_t)sys_eth_recv_check, "recv_check"},
1300 #ifdef __CONFIG_ARSC_SERVER__
1301 [SYS_init_arsc] = {(syscall_t)sys_init_arsc, "init_arsc"},
1303 [SYS_read] = {(syscall_t)sys_read, "read"},
1304 [SYS_write] = {(syscall_t)sys_write, "write"},
1305 [SYS_open] = {(syscall_t)sys_open, "open"},
1306 [SYS_close] = {(syscall_t)sys_close, "close"},
1307 [SYS_fstat] = {(syscall_t)sys_fstat, "fstat"},
1308 [SYS_stat] = {(syscall_t)sys_stat, "stat"},
1309 [SYS_lstat] = {(syscall_t)sys_lstat, "lstat"},
1310 [SYS_fcntl] = {(syscall_t)sys_fcntl, "fcntl"},
1311 [SYS_access] = {(syscall_t)sys_access, "access"},
1312 [SYS_umask] = {(syscall_t)sys_umask, "umask"},
1313 [SYS_chmod] = {(syscall_t)sys_chmod, "chmod"},
1314 [SYS_lseek] = {(syscall_t)sys_lseek, "lseek"},
1315 [SYS_link] = {(syscall_t)sys_link, "link"},
1316 [SYS_unlink] = {(syscall_t)sys_unlink, "unlink"},
1317 [SYS_symlink] = {(syscall_t)sys_symlink, "symlink"},
1318 [SYS_readlink] = {(syscall_t)sys_readlink, "readlink"},
1319 [SYS_chdir] = {(syscall_t)sys_chdir, "chdir"},
1320 [SYS_getcwd] = {(syscall_t)sys_getcwd, "getcwd"},
1321 [SYS_mkdir] = {(syscall_t)sys_mkdir, "mkdri"},
1322 [SYS_rmdir] = {(syscall_t)sys_rmdir, "rmdir"},
1323 [SYS_gettimeofday] = {(syscall_t)sys_gettimeofday, "gettime"},
1324 [SYS_tcgetattr] = {(syscall_t)sys_tcgetattr, "tcgetattr"},
1325 [SYS_tcsetattr] = {(syscall_t)sys_tcsetattr, "tcsetattr"},
1326 [SYS_setuid] = {(syscall_t)sys_setuid, "setuid"},
1327 [SYS_setgid] = {(syscall_t)sys_setgid, "setgid"}
1330 /* Executes the given syscall.
1332 * Note tf is passed in, which points to the tf of the context on the kernel
1333 * stack. If any syscall needs to block, it needs to save this info, as well as
1336 * This syscall function is used by both local syscall and arsc, and should
1337 * remain oblivious of the caller. */
1338 intreg_t syscall(struct proc *p, uintreg_t sc_num, uintreg_t a0, uintreg_t a1,
1339 uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
1341 const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
1343 uint32_t coreid, vcoreid;
1344 if (systrace_flags & SYSTRACE_ON) {
1345 if ((systrace_flags & SYSTRACE_ALLPROC) || (proc_is_traced(p))) {
1347 vcoreid = proc_get_vcoreid(p, coreid);
1348 if (systrace_flags & SYSTRACE_LOUD) {
1349 printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, "
1350 "%08p, %08p) proc: %d core: %d vcore: %d\n", read_tsc(),
1351 sc_num, syscall_table[sc_num].name, a0, a1, a2, a3,
1352 a4, a5, p->pid, coreid, vcoreid);
1354 struct systrace_record *trace;
1355 unsigned int idx, new_idx;
1357 idx = systrace_bufidx;
1358 new_idx = (idx + 1) % systrace_bufsize;
1359 } while (!atomic_comp_swap(&systrace_bufidx, idx, new_idx));
1360 trace = &systrace_buffer[idx];
1361 trace->timestamp = read_tsc();
1362 trace->syscallno = sc_num;
1369 trace->pid = p->pid;
1370 trace->coreid = coreid;
1371 trace->vcoreid = vcoreid;
1375 if (sc_num > max_syscall || syscall_table[sc_num].call == NULL)
1376 panic("Invalid syscall number %d for proc %x!", sc_num, *p);
1378 return syscall_table[sc_num].call(p, a0, a1, a2, a3, a4, a5);
1381 /* Execute the syscall on the local core */
1382 static void run_local_syscall(struct syscall *sysc)
1384 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
1386 /* TODO: (UMEM) assert / pin the memory for the sysc */
1387 user_mem_assert(pcpui->cur_proc, sysc, sizeof(struct syscall), PTE_USER_RW);
1388 pcpui->cur_sysc = sysc; /* let the core know which sysc it is */
1389 sysc->retval = syscall(pcpui->cur_proc, sysc->num, sysc->arg0, sysc->arg1,
1390 sysc->arg2, sysc->arg3, sysc->arg4, sysc->arg5);
1391 sysc->flags |= SC_DONE;
1392 /* Can unpin at this point */
1395 /* A process can trap and call this function, which will set up the core to
1396 * handle all the syscalls. a.k.a. "sys_debutante(needs, wants)". If there is
1397 * at least one, it will run it directly. */
1398 void prep_syscalls(struct proc *p, struct syscall *sysc, unsigned int nr_syscs)
1401 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
1404 /* For all after the first call, send ourselves a KMSG (TODO). */
1406 warn("Only one supported (Debutante calls: %d)\n", nr_syscs);
1407 /* Call the first one directly. (we already checked to make sure there is
1409 run_local_syscall(sysc);
1412 /* Syscall tracing */
1413 static void __init_systrace(void)
1415 systrace_buffer = kmalloc(MAX_SYSTRACES*sizeof(struct systrace_record), 0);
1416 if (!systrace_buffer)
1417 panic("Unable to alloc a trace buffer\n");
1418 systrace_bufidx = 0;
1419 systrace_bufsize = MAX_SYSTRACES;
1420 /* Note we never free the buffer - it's around forever. Feel free to change
1421 * this if you want to change the size or something dynamically. */
1424 /* If you call this while it is running, it will change the mode */
1425 void systrace_start(bool silent)
1427 static bool init = FALSE;
1428 spin_lock_irqsave(&systrace_lock);
1433 systrace_flags = silent ? SYSTRACE_ON : SYSTRACE_ON | SYSTRACE_LOUD;
1434 spin_unlock_irqsave(&systrace_lock);
1437 int systrace_reg(bool all, struct proc *p)
1440 spin_lock_irqsave(&systrace_lock);
1442 printk("Tracing syscalls for all processes\n");
1443 systrace_flags |= SYSTRACE_ALLPROC;
1446 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1447 if (!systrace_procs[i]) {
1448 printk("Tracing syscalls for process %d\n", p->pid);
1449 systrace_procs[i] = p;
1455 spin_unlock_irqsave(&systrace_lock);
1459 void systrace_stop(void)
1461 spin_lock_irqsave(&systrace_lock);
1463 for (int i = 0; i < MAX_NUM_TRACED; i++)
1464 systrace_procs[i] = 0;
1465 spin_unlock_irqsave(&systrace_lock);
1468 /* If you registered a process specifically, then you need to dereg it
1469 * specifically. Or just fully stop, which will do it for all. */
1470 int systrace_dereg(bool all, struct proc *p)
1472 spin_lock_irqsave(&systrace_lock);
1474 printk("No longer tracing syscalls for all processes.\n");
1475 systrace_flags &= ~SYSTRACE_ALLPROC;
1477 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1478 if (systrace_procs[i] == p) {
1479 systrace_procs[i] = 0;
1480 printk("No longer tracing syscalls for process %d\n", p->pid);
1484 spin_unlock_irqsave(&systrace_lock);
1488 /* Regardless of locking, someone could be writing into the buffer */
1489 void systrace_print(bool all, struct proc *p)
1491 spin_lock_irqsave(&systrace_lock);
1492 /* if you want to be clever, you could make this start from the earliest
1493 * timestamp and loop around. Careful of concurrent writes. */
1494 for (int i = 0; i < systrace_bufsize; i++)
1495 if (systrace_buffer[i].timestamp)
1496 printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, %08p,"
1497 "%08p) proc: %d core: %d vcore: %d\n",
1498 systrace_buffer[i].timestamp,
1499 systrace_buffer[i].syscallno,
1500 syscall_table[systrace_buffer[i].syscallno].name,
1501 systrace_buffer[i].arg0,
1502 systrace_buffer[i].arg1,
1503 systrace_buffer[i].arg2,
1504 systrace_buffer[i].arg3,
1505 systrace_buffer[i].arg4,
1506 systrace_buffer[i].arg5,
1507 systrace_buffer[i].pid,
1508 systrace_buffer[i].coreid,
1509 systrace_buffer[i].vcoreid);
1510 spin_unlock_irqsave(&systrace_lock);
1513 void systrace_clear_buffer(void)
1515 spin_lock_irqsave(&systrace_lock);
1516 memset(systrace_buffer, 0, sizeof(struct systrace_record) * MAX_SYSTRACES);
1517 spin_unlock_irqsave(&systrace_lock);