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>
33 #include <kfs.h> // eventually replace this with vfs.h
35 #include <arsc_server.h>
38 #ifdef __CONFIG_NETWORKING__
39 #include <arch/nic_common.h>
40 extern int (*send_frame)(const char *CT(len) data, size_t len);
41 extern unsigned char device_mac[6];
45 int systrace_flags = 0;
46 struct systrace_record *systrace_buffer = 0;
47 unsigned int systrace_bufidx = 0;
48 size_t systrace_bufsize = 0;
49 struct proc *systrace_procs[MAX_NUM_TRACED] = {0};
50 spinlock_t systrace_lock = SPINLOCK_INITIALIZER;
52 /* Not enforcing the packing of systrace_procs yet, but don't rely on that */
53 static bool proc_is_traced(struct proc *p)
55 for (int i = 0; i < MAX_NUM_TRACED; i++)
56 if (systrace_procs[i] == p)
61 /************** Utility Syscalls **************/
63 static int sys_null(void)
68 // Writes 'val' to 'num_writes' entries of the well-known array in the kernel
69 // address space. It's just #defined to be some random 4MB chunk (which ought
70 // to be boot_alloced or something). Meant to grab exclusive access to cache
71 // lines, to simulate doing something useful.
72 static int sys_cache_buster(struct proc *p, uint32_t num_writes,
73 uint32_t num_pages, uint32_t flags)
74 { TRUSTEDBLOCK /* zra: this is not really part of the kernel */
75 #define BUSTER_ADDR 0xd0000000 // around 512 MB deep
76 #define MAX_WRITES 1048576*8
78 #define INSERT_ADDR (UINFO + 2*PGSIZE) // should be free for these tests
79 uint32_t* buster = (uint32_t*)BUSTER_ADDR;
80 static spinlock_t buster_lock = SPINLOCK_INITIALIZER;
82 page_t* a_page[MAX_PAGES];
84 /* Strided Accesses or Not (adjust to step by cachelines) */
86 if (flags & BUSTER_STRIDED) {
91 /* Shared Accesses or Not (adjust to use per-core regions)
92 * Careful, since this gives 8MB to each core, starting around 512MB.
93 * Also, doesn't separate memory for core 0 if it's an async call.
95 if (!(flags & BUSTER_SHARED))
96 buster = (uint32_t*)(BUSTER_ADDR + core_id() * 0x00800000);
98 /* Start the timer, if we're asked to print this info*/
99 if (flags & BUSTER_PRINT_TICKS)
100 ticks = start_timing();
102 /* Allocate num_pages (up to MAX_PAGES), to simulate doing some more
103 * realistic work. Note we don't write to these pages, even if we pick
104 * unshared. Mostly due to the inconvenience of having to match up the
105 * number of pages with the number of writes. And it's unnecessary.
108 spin_lock(&buster_lock);
109 for (int i = 0; i < MIN(num_pages, MAX_PAGES); i++) {
110 upage_alloc(p, &a_page[i],1);
111 page_insert(p->env_pgdir, a_page[i], (void*)INSERT_ADDR + PGSIZE*i,
114 spin_unlock(&buster_lock);
117 if (flags & BUSTER_LOCKED)
118 spin_lock(&buster_lock);
119 for (int i = 0; i < MIN(num_writes, MAX_WRITES); i=i+stride)
120 buster[i] = 0xdeadbeef;
121 if (flags & BUSTER_LOCKED)
122 spin_unlock(&buster_lock);
125 spin_lock(&buster_lock);
126 for (int i = 0; i < MIN(num_pages, MAX_PAGES); i++) {
127 page_remove(p->env_pgdir, (void*)(INSERT_ADDR + PGSIZE * i));
128 page_decref(a_page[i]);
130 spin_unlock(&buster_lock);
134 if (flags & BUSTER_PRINT_TICKS) {
135 ticks = stop_timing(ticks);
136 printk("%llu,", ticks);
141 static int sys_cache_invalidate(void)
149 /* sys_reboot(): called directly from dispatch table. */
151 /* Print a string to the system console. */
152 static ssize_t sys_cputs(struct proc *p, const char *DANGEROUS string,
156 t_string = user_strdup_errno(p, string, strlen);
159 printk("%.*s", strlen, t_string);
160 user_memdup_free(p, t_string);
161 return (ssize_t)strlen;
164 // Read a character from the system console.
165 // Returns the character.
166 static uint16_t sys_cgetc(struct proc *p)
170 // The cons_getc() primitive doesn't wait for a character,
171 // but the sys_cgetc() system call does.
172 while ((c = cons_getc()) == 0)
178 /* Returns the id of the cpu this syscall is executed on. */
179 static uint32_t sys_getcpuid(void)
184 // TODO: Temporary hack until thread-local storage is implemented on i386 and
185 // this is removed from the user interface
186 static size_t sys_getvcoreid(struct proc *p)
188 return proc_get_vcoreid(p, core_id());
191 /************** Process management syscalls **************/
193 /* Returns the calling process's pid */
194 static pid_t sys_getpid(struct proc *p)
199 /* Creates a process from the file 'path'. The process is not runnable by
200 * default, so it needs it's status to be changed so that the next call to
201 * schedule() will try to run it. TODO: take args/envs from userspace. */
202 static int sys_proc_create(struct proc *p, char *path, size_t path_l,
207 struct file *program;
210 /* Copy in the path. Consider putting an upper bound on path_l. */
211 t_path = user_strdup_errno(p, path, path_l);
214 program = do_file_open(t_path, 0, 0);
215 user_memdup_free(p, t_path);
217 return -1; /* presumably, errno is already set */
218 /* TODO: need to split the proc creation, since you must load after setting
219 * args/env, since auxp gets set up there. */
220 //new_p = proc_create(program, 0, 0);
221 if (proc_alloc(&new_p, current))
223 /* Set the argument stuff needed by glibc */
224 if (memcpy_from_user_errno(p, new_p->procinfo->argp, pi->argp,
227 if (memcpy_from_user_errno(p, new_p->procinfo->argbuf, pi->argbuf,
230 if (load_elf(new_p, program))
232 kref_put(&program->f_kref);
235 kref_put(&new_p->kref); /* give up the reference created in proc_create() */
240 kref_put(&program->f_kref);
244 /* Makes process PID runnable. Consider moving the functionality to process.c */
245 static error_t sys_proc_run(struct proc *p, unsigned pid)
247 struct proc *target = pid2proc(pid);
252 // note we can get interrupted here. it's not bad.
253 spin_lock(&p->proc_lock);
254 // make sure we have access and it's in the right state to be activated
255 if (!proc_controls(p, target)) {
256 kref_put(&target->kref);
258 } else if (target->state != PROC_CREATED) {
259 kref_put(&target->kref);
262 __proc_set_state(target, PROC_RUNNABLE_S);
263 schedule_proc(target);
265 spin_unlock(&p->proc_lock);
266 kref_put(&target->kref);
270 /* Destroy proc pid. If this is called by the dying process, it will never
271 * return. o/w it will return 0 on success, or an error. Errors include:
272 * - EBADPROC: if there is no such process with pid
273 * - EPERM: if caller does not control pid */
274 static error_t sys_proc_destroy(struct proc *p, pid_t pid, int exitcode)
277 struct proc *p_to_die = pid2proc(pid);
283 if (!proc_controls(p, p_to_die)) {
284 kref_put(&p_to_die->kref);
289 // syscall code and pid2proc both have edible references, only need 1.
290 p->exitcode = exitcode;
291 kref_put(&p_to_die->kref);
292 printd("[PID %d] proc exiting gracefully (code %d)\n", p->pid,exitcode);
294 printd("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
296 proc_destroy(p_to_die);
297 kref_put(&p_to_die->kref);
301 static int sys_proc_yield(struct proc *p, bool being_nice)
303 proc_yield(p, being_nice);
307 static ssize_t sys_fork(env_t* e)
309 // TODO: right now we only support fork for single-core processes
310 if (e->state != PROC_RUNNING_S) {
314 /* Can't really fork if we don't have a current_tf to fork */
320 assert(!proc_alloc(&env, current));
323 env->heap_top = e->heap_top;
325 env->env_tf = *current_tf;
327 env->cache_colors_map = cache_colors_map_alloc();
328 for(int i=0; i < llc_cache->num_colors; i++)
329 if(GET_BITMASK_BIT(e->cache_colors_map,i))
330 cache_color_alloc(llc_cache, env->cache_colors_map);
332 duplicate_vmrs(e, env);
334 int copy_page(env_t* e, pte_t* pte, void* va, void* arg)
336 env_t* env = (env_t*)arg;
338 if(PAGE_PRESENT(*pte))
341 if(upage_alloc(env,&pp,0))
343 if(page_insert(env->env_pgdir,pp,va,*pte & PTE_PERM))
349 pagecopy(page2kva(pp),ppn2kva(PTE2PPN(*pte)));
351 assert(PAGE_PAGED_OUT(*pte));
352 /* TODO: (SWAP) will need to either make a copy or CoW/refcnt the
353 * backend store. For now, this PTE will be the same as the
355 panic("Swapping not supported!");
356 pte_t* newpte = pgdir_walk(env->env_pgdir,va,1);
364 // TODO: (PC) this won't work. Needs revisiting.
365 // copy procdata and procinfo
366 memcpy(env->procdata,e->procdata,sizeof(struct procdata));
367 memcpy(env->procinfo,e->procinfo,sizeof(struct procinfo));
368 env->procinfo->pid = env->pid;
369 env->procinfo->ppid = env->ppid;
371 /* for now, just copy the contents of every present page in the entire
373 if (env_user_mem_walk(e, 0, UMAPTOP, ©_page, env)) {
374 proc_destroy(env); /* this is prob what you want, not decref by 2 */
378 clone_files(&e->open_files, &env->open_files);
380 __proc_set_state(env, PROC_RUNNABLE_S);
383 // don't decref the new process.
384 // that will happen when the parent waits for it.
385 // TODO: if the parent doesn't wait, we need to change the child's parent
386 // when the parent dies, or at least decref it
388 printd("[PID %d] fork PID %d\n",e->pid,env->pid);
393 /* Load the binary "path" into the current process, and start executing it.
394 * argv and envp are magically bundled in procinfo for now. Keep in sync with
395 * glibc's sysdeps/ros/execve.c */
396 static int sys_exec(struct proc *p, char *path, size_t path_l,
401 struct file *program;
403 /* We probably want it to never be allowed to exec if it ever was _M */
404 if (p->state != PROC_RUNNING_S) {
408 /* Can't really exec if we don't have a current_tf to reset */
413 /* Copy in the path. Consider putting an upper bound on path_l. */
414 t_path = user_strdup_errno(p, path, path_l);
417 program = do_file_open(t_path, 0, 0);
418 user_memdup_free(p, t_path);
420 return -1; /* presumably, errno is already set */
421 /* Set the argument stuff needed by glibc */
422 if (memcpy_from_user_errno(p, p->procinfo->argp, pi->argp,
425 if (memcpy_from_user_errno(p, p->procinfo->argbuf, pi->argbuf,
428 /* This is the point of no return for the process. */
429 /* TODO: issues with this: Need to also assert there are no outstanding
430 * users of the sysrings. the ldt page will get freed shortly, so that's
431 * okay. Potentially issues with the nm and vcpd if we were in _M before
432 * and someone is trying to notify. */
433 memset(p->procdata, 0, sizeof(procdata_t));
435 close_all_files(&p->open_files, TRUE);
436 env_user_mem_free(p, 0, UMAPTOP);
437 if (load_elf(p, program)) {
438 kref_put(&program->f_kref);
440 smp_idle(); /* syscall can't return on failure now */
442 printd("[PID %d] exec %s\n", p->pid, file_name(program));
443 kref_put(&program->f_kref);
444 *current_tf = p->env_tf;
447 kref_put(&program->f_kref);
451 static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
453 struct proc* p = pid2proc(pid);
455 // TODO: this syscall is racy, so we only support for single-core procs
456 if(e->state != PROC_RUNNING_S)
459 // TODO: need to use errno properly. sadly, ROS error codes conflict..
465 if(current->pid == p->ppid)
467 if(p->state == PROC_DYING)
469 memcpy_to_user(e,status,&p->exitcode,sizeof(int));
470 printd("[PID %d] waited for PID %d (code %d)\n",
471 e->pid,p->pid,p->exitcode);
480 else // not a child of the calling process
486 // if the wait succeeded, decref twice
497 /************** Memory Management Syscalls **************/
499 static void *sys_mmap(struct proc *p, uintreg_t a1, uintreg_t a2, uintreg_t a3,
503 if (memcpy_from_user(p, _a456, a456, 3 * sizeof(uintreg_t)))
504 sys_proc_destroy(p, p->pid, -1);
505 return mmap(p, a1, a2, a3, _a456[0], _a456[1], _a456[2]);
508 static intreg_t sys_mprotect(struct proc *p, void *addr, size_t len, int prot)
510 return mprotect(p, (uintptr_t)addr, len, prot);
513 static intreg_t sys_munmap(struct proc *p, void *addr, size_t len)
515 return munmap(p, (uintptr_t)addr, len);
518 static ssize_t sys_shared_page_alloc(env_t* p1,
519 void**DANGEROUS _addr, pid_t p2_id,
520 int p1_flags, int p2_flags
523 /* When we remove/change this, also get rid of page_insert_in_range() */
524 printk("[kernel] the current shared page alloc is deprecated.\n");
525 //if (!VALID_USER_PERMS(p1_flags)) return -EPERM;
526 //if (!VALID_USER_PERMS(p2_flags)) return -EPERM;
528 void * COUNT(1) * COUNT(1) addr = user_mem_assert(p1, _addr, sizeof(void *),
530 struct proc *p2 = pid2proc(p2_id);
535 error_t e = upage_alloc(p1, &page,1);
541 void* p2_addr = page_insert_in_range(p2->env_pgdir, page,
542 (void*SNT)UTEXT, (void*SNT)UTOP, p2_flags);
543 if (p2_addr == NULL) {
549 void* p1_addr = page_insert_in_range(p1->env_pgdir, page,
550 (void*SNT)UTEXT, (void*SNT)UTOP, p1_flags);
551 if(p1_addr == NULL) {
552 page_remove(p2->env_pgdir, p2_addr);
562 static int sys_shared_page_free(env_t* p1, void*DANGEROUS addr, pid_t p2)
568 /* sys_resource_req(): called directly from dispatch table. */
570 /* Will notify the target on the given vcore, if the caller controls the target.
571 * Will honor the target's wanted/vcoreid. u_ne can be NULL. */
572 static int sys_notify(struct proc *p, int target_pid, unsigned int notif,
573 struct notif_event *u_ne)
575 struct notif_event local_ne;
576 struct proc *target = pid2proc(target_pid);
582 if (!proc_controls(p, target)) {
583 kref_put(&target->kref);
587 /* if the user provided a notif_event, copy it in and use that */
589 if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
590 kref_put(&target->kref);
594 proc_notify(target, local_ne.ne_type, &local_ne);
596 proc_notify(target, notif, 0);
598 kref_put(&target->kref);
602 /* Will notify the calling process on the given vcore, independently of WANTED
603 * or advertised vcoreid. If you change the parameters, change pop_ros_tf() */
604 static int sys_self_notify(struct proc *p, uint32_t vcoreid, unsigned int notif,
605 struct notif_event *u_ne)
607 struct notif_event local_ne;
609 printd("[kernel] received self notify for vcoreid %d, notif %d, ne %08p\n",
610 vcoreid, notif, u_ne);
611 /* if the user provided a notif_event, copy it in and use that */
613 if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
617 do_notify(p, vcoreid, local_ne.ne_type, &local_ne);
619 do_notify(p, vcoreid, notif, 0);
624 /* This will set a local timer for usec, then shut down the core */
625 static int sys_halt_core(struct proc *p, unsigned int usec)
627 /* TODO: ought to check and see if a timer was already active, etc, esp so
628 * userspace can't turn off timers. also note we will also call whatever
629 * timer_interrupt() will do, though all we care about is just
630 * self_ipi/interrupting. */
631 set_core_timer(usec);
637 /************** Platform Specific Syscalls **************/
639 //Read a buffer over the serial port
640 static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
642 printk("[kernel] serial reading is deprecated.\n");
646 #ifdef __CONFIG_SERIAL_IO__
647 char *COUNT(len) buf = user_mem_assert(e, _buf, len, PTE_USER_RO);
648 size_t bytes_read = 0;
650 while((c = serial_read_byte()) != -1) {
651 buf[bytes_read++] = (uint8_t)c;
652 if(bytes_read == len) break;
654 return (ssize_t)bytes_read;
660 //Write a buffer over the serial port
661 static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
663 printk("[kernel] serial writing is deprecated.\n");
666 #ifdef __CONFIG_SERIAL_IO__
667 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_USER_RO);
668 for(int i =0; i<len; i++)
669 serial_send_byte(buf[i]);
676 #ifdef __CONFIG_NETWORKING__
677 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
678 static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
685 spin_lock(&packet_buffers_lock);
687 if (num_packet_buffers == 0) {
688 spin_unlock(&packet_buffers_lock);
692 ptr = packet_buffers[packet_buffers_head];
693 len = packet_buffers_sizes[packet_buffers_head];
695 num_packet_buffers--;
696 packet_buffers_head = (packet_buffers_head + 1) % MAX_PACKET_BUFFERS;
698 spin_unlock(&packet_buffers_lock);
700 char* _buf = user_mem_assert(e, buf, len, PTE_U);
702 memcpy(_buf, ptr, len);
712 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
713 static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
720 // HACK TO BYPASS HACK
721 int just_sent = send_frame(buf, len);
724 printk("Packet send fail\n");
730 // END OF RECURSIVE HACK
732 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_U);
735 int cur_packet_len = 0;
736 while (total_sent != len) {
737 cur_packet_len = ((len - total_sent) > MTU) ? MTU : (len - total_sent);
738 char dest_mac[6] = APPSERVER_MAC_ADDRESS;
739 char* wrap_buffer = eth_wrap(_buf + total_sent, cur_packet_len, device_mac, dest_mac, APPSERVER_PORT);
740 just_sent = send_frame(wrap_buffer, cur_packet_len + sizeof(struct ETH_Header));
743 return 0; // This should be an error code of its own
748 total_sent += cur_packet_len;
758 static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf)
761 for (int i = 0; i < 6; i++)
762 buf[i] = device_mac[i];
769 static int sys_eth_recv_check(env_t* e)
771 if (num_packet_buffers != 0)
779 static intreg_t sys_read(struct proc *p, int fd, void *buf, int len)
782 struct file *file = get_file_from_fd(&p->open_files, fd);
787 assert(file->f_op->read);
788 /* TODO: (UMEM) currently, read() handles user memcpy issues, but we
789 * probably should user_mem_check and pin the region here, so read doesn't
791 ret = file->f_op->read(file, buf, len, &file->f_pos);
792 kref_put(&file->f_kref);
796 static intreg_t sys_write(struct proc *p, int fd, const void *buf, int len)
798 /* Catch common usage of stdout and stderr. No protections or anything. */
800 printk("[stdout]: %s\n", buf);
802 } else if (fd == 2) {
803 printk("[stderr]: %s\n", buf);
806 /* the real sys_write: */
808 struct file *file = get_file_from_fd(&p->open_files, fd);
813 if (!file->f_op->write) {
814 kref_put(&file->f_kref);
819 ret = file->f_op->write(file, buf, len, &file->f_pos);
820 kref_put(&file->f_kref);
824 /* Checks args/reads in the path, opens the file, and inserts it into the
825 * process's open file list.
827 * TODO: take the path length */
828 static intreg_t sys_open(struct proc *p, const char *path, size_t path_l,
834 char *t_path = user_strdup_errno(p, path, path_l);
837 file = do_file_open(t_path, oflag, mode);
838 user_memdup_free(p, t_path);
841 fd = insert_file(&p->open_files, file); /* stores the ref to file */
842 kref_put(&file->f_kref);
844 warn("File insertion failed");
847 printd("File %s Open, res=%d\n", path, fd);
851 static intreg_t sys_close(struct proc *p, int fd)
853 struct file *file = put_file_from_fd(&p->open_files, fd);
861 /* kept around til we remove the last ufe */
862 #define ufe(which,a0,a1,a2,a3) \
863 frontend_syscall_errno(p,APPSERVER_SYSCALL_##which,\
864 (int)(a0),(int)(a1),(int)(a2),(int)(a3))
866 static intreg_t sys_fstat(struct proc *p, int fd, struct kstat *u_stat)
869 struct file *file = get_file_from_fd(&p->open_files, fd);
874 kbuf = kmalloc(sizeof(struct kstat), 0);
876 kref_put(&file->f_kref);
880 stat_inode(file->f_dentry->d_inode, kbuf);
881 kref_put(&file->f_kref);
882 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
883 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
892 /* sys_stat() and sys_lstat() do nearly the same thing, differing in how they
893 * treat a symlink for the final item, which (probably) will be controlled by
894 * the lookup flags */
895 static intreg_t stat_helper(struct proc *p, const char *path, size_t path_l,
896 struct kstat *u_stat, int flags)
899 struct dentry *path_d;
900 char *t_path = user_strdup_errno(p, path, path_l);
903 path_d = lookup_dentry(t_path, flags);
904 user_memdup_free(p, t_path);
907 kbuf = kmalloc(sizeof(struct kstat), 0);
910 kref_put(&path_d->d_kref);
913 stat_inode(path_d->d_inode, kbuf);
914 kref_put(&path_d->d_kref);
915 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
916 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
925 /* Follow a final symlink */
926 static intreg_t sys_stat(struct proc *p, const char *path, size_t path_l,
927 struct kstat *u_stat)
929 return stat_helper(p, path, path_l, u_stat, LOOKUP_FOLLOW);
932 /* Don't follow a final symlink */
933 static intreg_t sys_lstat(struct proc *p, const char *path, size_t path_l,
934 struct kstat *u_stat)
936 return stat_helper(p, path, path_l, u_stat, 0);
939 intreg_t sys_fcntl(struct proc *p, int fd, int cmd, int arg)
942 struct file *file = get_file_from_fd(&p->open_files, fd);
949 printk("[kernel] dup not supported yet\n");
952 /* GET and SETFD just care about CLOEXEC. We don't have a separate
953 * flag variable for the FD (we might need to, technically). */
954 if (file->f_flags & O_CLOEXEC)
958 if (arg == FD_CLOEXEC)
959 file->f_flags |= O_CLOEXEC;
962 retval = file->f_flags;
965 /* only allowed to set certain flags. */
966 arg &= O_APPEND | O_ASYNC | O_DIRECT | O_NOATIME | O_NONBLOCK;
969 warn("Unsupported fcntl cmd %d\n", cmd);
971 kref_put(&file->f_kref);
975 static intreg_t sys_access(struct proc *p, const char *path, size_t path_l,
979 char *t_path = user_strdup_errno(p, path, path_l);
982 retval = do_file_access(t_path, mode);
983 user_memdup_free(p, t_path);
984 printd("Access for path: %s retval: %d\n", path, retval);
992 intreg_t sys_umask(struct proc *p, int mask)
994 int old_mask = p->fs_env.umask;
995 p->fs_env.umask = mask & 0777;
999 intreg_t sys_chmod(struct proc *p, const char *path, size_t path_l, int mode)
1001 char* fn = user_strdup_errno(p,path,PGSIZE);
1004 int ret = ufe(chmod,PADDR(fn),mode,0,0);
1005 user_memdup_free(p,fn);
1009 static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
1012 struct file *file = get_file_from_fd(&p->open_files, fd);
1017 ret = file->f_op->llseek(file, offset, whence);
1018 kref_put(&file->f_kref);
1022 intreg_t sys_link(struct proc *p, char *old_path, size_t old_l,
1023 char *new_path, size_t new_l)
1026 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1027 if (t_oldpath == NULL)
1029 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1030 if (t_newpath == NULL) {
1031 user_memdup_free(p, t_oldpath);
1034 ret = do_link(t_oldpath, t_newpath);
1035 user_memdup_free(p, t_oldpath);
1036 user_memdup_free(p, t_newpath);
1040 intreg_t sys_unlink(struct proc *p, const char *path, size_t path_l)
1043 char *t_path = user_strdup_errno(p, path, path_l);
1046 retval = do_unlink(t_path);
1047 user_memdup_free(p, t_path);
1051 intreg_t sys_symlink(struct proc *p, char *old_path, size_t old_l,
1052 char *new_path, size_t new_l)
1055 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1056 if (t_oldpath == NULL)
1058 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1059 if (t_newpath == NULL) {
1060 user_memdup_free(p, t_oldpath);
1063 ret = do_symlink(new_path, old_path, S_IRWXU | S_IRWXG | S_IRWXO);
1064 user_memdup_free(p, t_oldpath);
1065 user_memdup_free(p, t_newpath);
1069 intreg_t sys_readlink(struct proc *p, char *path, size_t path_l,
1070 char *u_buf, size_t buf_l)
1074 struct dentry *path_d;
1075 char *t_path = user_strdup_errno(p, path, path_l);
1078 path_d = lookup_dentry(t_path, 0);
1079 user_memdup_free(p, t_path);
1082 symname = path_d->d_inode->i_op->readlink(path_d);
1083 copy_amt = strnlen(symname, buf_l - 1) + 1;
1084 if (memcpy_to_user_errno(p, u_buf, symname, copy_amt)) {
1085 kref_put(&path_d->d_kref);
1089 kref_put(&path_d->d_kref);
1090 printd("READLINK returning %s\n", u_buf);
1094 intreg_t sys_chdir(struct proc *p, const char *path, size_t path_l)
1097 char *t_path = user_strdup_errno(p, path, path_l);
1100 retval = do_chdir(&p->fs_env, t_path);
1101 user_memdup_free(p, t_path);
1109 /* Note cwd_l is not a strlen, it's an absolute size */
1110 intreg_t sys_getcwd(struct proc *p, char *u_cwd, size_t cwd_l)
1114 char *k_cwd = do_getcwd(&p->fs_env, &kfree_this, cwd_l);
1116 return -1; /* errno set by do_getcwd */
1117 if (memcpy_to_user_errno(p, u_cwd, k_cwd, strnlen(k_cwd, cwd_l - 1) + 1))
1123 intreg_t sys_gettimeofday(struct proc *p, int *buf)
1125 static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
1128 spin_lock(>od_lock);
1131 #if (defined __CONFIG_APPSERVER__)
1132 t0 = ufe(time,0,0,0,0);
1134 // Nanwan's birthday, bitches!!
1137 spin_unlock(>od_lock);
1139 long long dt = read_tsc();
1140 int kbuf[2] = {t0+dt/system_timing.tsc_freq,
1141 (dt%system_timing.tsc_freq)*1000000/system_timing.tsc_freq};
1143 return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
1146 #define SIZEOF_STRUCT_TERMIOS 60
1147 intreg_t sys_tcgetattr(struct proc *p, int fd, void *termios_p)
1149 int* kbuf = kmalloc(SIZEOF_STRUCT_TERMIOS,0);
1150 int ret = ufe(tcgetattr,fd,PADDR(kbuf),0,0);
1151 if(ret != -1 && memcpy_to_user_errno(p,termios_p,kbuf,SIZEOF_STRUCT_TERMIOS))
1157 intreg_t sys_tcsetattr(struct proc *p, int fd, int optional_actions,
1158 const void *termios_p)
1160 void* kbuf = user_memdup_errno(p,termios_p,SIZEOF_STRUCT_TERMIOS);
1163 int ret = ufe(tcsetattr,fd,optional_actions,PADDR(kbuf),0);
1164 user_memdup_free(p,kbuf);
1168 /************** Syscall Invokation **************/
1170 /* Executes the given syscall.
1172 * Note tf is passed in, which points to the tf of the context on the kernel
1173 * stack. If any syscall needs to block, it needs to save this info, as well as
1176 * This syscall function is used by both local syscall and arsc, and should
1177 * remain oblivious of the caller. */
1178 intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
1179 uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
1181 /* Initialize the return value and error code returned to 0 */
1182 set_retval(ESUCCESS);
1183 set_errno(ESUCCESS);
1185 typedef intreg_t (*syscall_t)(struct proc*,uintreg_t,uintreg_t,
1186 uintreg_t,uintreg_t,uintreg_t);
1188 const static syscall_t syscall_table[] = {
1189 [SYS_null] = (syscall_t)sys_null,
1190 [SYS_cache_buster] = (syscall_t)sys_cache_buster,
1191 [SYS_cache_invalidate] = (syscall_t)sys_cache_invalidate,
1192 [SYS_reboot] = (syscall_t)reboot,
1193 [SYS_cputs] = (syscall_t)sys_cputs,
1194 [SYS_cgetc] = (syscall_t)sys_cgetc,
1195 [SYS_getcpuid] = (syscall_t)sys_getcpuid,
1196 [SYS_getvcoreid] = (syscall_t)sys_getvcoreid,
1197 [SYS_getpid] = (syscall_t)sys_getpid,
1198 [SYS_proc_create] = (syscall_t)sys_proc_create,
1199 [SYS_proc_run] = (syscall_t)sys_proc_run,
1200 [SYS_proc_destroy] = (syscall_t)sys_proc_destroy,
1201 [SYS_yield] = (syscall_t)sys_proc_yield,
1202 [SYS_fork] = (syscall_t)sys_fork,
1203 [SYS_exec] = (syscall_t)sys_exec,
1204 [SYS_trywait] = (syscall_t)sys_trywait,
1205 [SYS_mmap] = (syscall_t)sys_mmap,
1206 [SYS_munmap] = (syscall_t)sys_munmap,
1207 [SYS_mprotect] = (syscall_t)sys_mprotect,
1208 [SYS_shared_page_alloc] = (syscall_t)sys_shared_page_alloc,
1209 [SYS_shared_page_free] = (syscall_t)sys_shared_page_free,
1210 [SYS_resource_req] = (syscall_t)resource_req,
1211 [SYS_notify] = (syscall_t)sys_notify,
1212 [SYS_self_notify] = (syscall_t)sys_self_notify,
1213 [SYS_halt_core] = (syscall_t)sys_halt_core,
1214 #ifdef __CONFIG_SERIAL_IO__
1215 [SYS_serial_read] = (syscall_t)sys_serial_read,
1216 [SYS_serial_write] = (syscall_t)sys_serial_write,
1218 #ifdef __CONFIG_NETWORKING__
1219 [SYS_eth_read] = (syscall_t)sys_eth_read,
1220 [SYS_eth_write] = (syscall_t)sys_eth_write,
1221 [SYS_eth_get_mac_addr] = (syscall_t)sys_eth_get_mac_addr,
1222 [SYS_eth_recv_check] = (syscall_t)sys_eth_recv_check,
1224 #ifdef __CONFIG_ARSC_SERVER__
1225 [SYS_init_arsc] = (syscall_t)sys_init_arsc,
1227 // Syscalls serviced by the appserver for now.
1228 [SYS_read] = (syscall_t)sys_read,
1229 [SYS_write] = (syscall_t)sys_write,
1230 [SYS_open] = (syscall_t)sys_open,
1231 [SYS_close] = (syscall_t)sys_close,
1232 [SYS_fstat] = (syscall_t)sys_fstat,
1233 [SYS_stat] = (syscall_t)sys_stat,
1234 [SYS_lstat] = (syscall_t)sys_lstat,
1235 [SYS_fcntl] = (syscall_t)sys_fcntl,
1236 [SYS_access] = (syscall_t)sys_access,
1237 [SYS_umask] = (syscall_t)sys_umask,
1238 [SYS_chmod] = (syscall_t)sys_chmod,
1239 [SYS_lseek] = (syscall_t)sys_lseek,
1240 [SYS_link] = (syscall_t)sys_link,
1241 [SYS_unlink] = (syscall_t)sys_unlink,
1242 [SYS_symlink] = (syscall_t)sys_symlink,
1243 [SYS_readlink] = (syscall_t)sys_readlink,
1244 [SYS_chdir] = (syscall_t)sys_chdir,
1245 [SYS_getcwd] = (syscall_t)sys_getcwd,
1246 [SYS_gettimeofday] = (syscall_t)sys_gettimeofday,
1247 [SYS_tcgetattr] = (syscall_t)sys_tcgetattr,
1248 [SYS_tcsetattr] = (syscall_t)sys_tcsetattr
1251 const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
1253 uint32_t coreid, vcoreid;
1254 if (systrace_flags & SYSTRACE_ON) {
1255 if ((systrace_flags & SYSTRACE_ALLPROC) || (proc_is_traced(p))) {
1257 vcoreid = proc_get_vcoreid(p, core_id());
1258 if (systrace_flags & SYSTRACE_LOUD) {
1259 printk("[%16llu] Syscall %d for proc %d on core %d, vcore %d\n",
1260 read_tsc(), syscallno, p->pid, coreid, vcoreid);
1262 struct systrace_record *trace;
1263 unsigned int idx, new_idx;
1265 idx = systrace_bufidx;
1266 new_idx = (idx + 1) % systrace_bufsize;
1267 } while (!atomic_comp_swap(&systrace_bufidx, idx, new_idx));
1268 trace = &systrace_buffer[idx];
1269 trace->timestamp = read_tsc();
1270 trace->syscallno = syscallno;
1271 trace->pid = p->pid;
1272 trace->coreid = coreid;
1273 trace->vcoreid = vcoreid;
1277 //printk("Incoming syscall on core: %d number: %d\n a1: %x\n "
1278 // " a2: %x\n a3: %x\n a4: %x\n a5: %x\n", core_id(),
1279 // syscallno, a1, a2, a3, a4, a5);
1281 if(syscallno > max_syscall || syscall_table[syscallno] == NULL)
1282 panic("Invalid syscall number %d for proc %x!", syscallno, *p);
1284 return syscall_table[syscallno](p,a1,a2,a3,a4,a5);
1287 /* Syscall tracing */
1288 static void __init_systrace(void)
1290 systrace_buffer = kmalloc(MAX_SYSTRACES*sizeof(struct systrace_record), 0);
1291 if (!systrace_buffer)
1292 panic("Unable to alloc a trace buffer\n");
1293 systrace_bufidx = 0;
1294 systrace_bufsize = MAX_SYSTRACES;
1295 /* Note we never free the buffer - it's around forever. Feel free to change
1296 * this if you want to change the size or something dynamically. */
1299 /* If you call this while it is running, it will change the mode */
1300 void systrace_start(bool silent)
1302 static bool init = FALSE;
1303 spin_lock_irqsave(&systrace_lock);
1308 systrace_flags = silent ? SYSTRACE_ON : SYSTRACE_ON | SYSTRACE_LOUD;
1309 spin_unlock_irqsave(&systrace_lock);
1312 int systrace_reg(bool all, struct proc *p)
1315 spin_lock_irqsave(&systrace_lock);
1317 printk("Tracing syscalls for all processes\n");
1318 systrace_flags |= SYSTRACE_ALLPROC;
1321 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1322 if (!systrace_procs[i]) {
1323 printk("Tracing syscalls for process %d\n", p->pid);
1324 systrace_procs[i] = p;
1330 spin_unlock_irqsave(&systrace_lock);
1334 void systrace_stop(void)
1336 spin_lock_irqsave(&systrace_lock);
1338 for (int i = 0; i < MAX_NUM_TRACED; i++)
1339 systrace_procs[i] = 0;
1340 spin_unlock_irqsave(&systrace_lock);
1343 /* If you registered a process specifically, then you need to dereg it
1344 * specifically. Or just fully stop, which will do it for all. */
1345 int systrace_dereg(bool all, struct proc *p)
1347 spin_lock_irqsave(&systrace_lock);
1349 printk("No longer tracing syscalls for all processes.\n");
1350 systrace_flags &= ~SYSTRACE_ALLPROC;
1352 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1353 if (systrace_procs[i] == p) {
1354 systrace_procs[i] = 0;
1355 printk("No longer tracing syscalls for process %d\n", p->pid);
1359 spin_unlock_irqsave(&systrace_lock);
1363 /* Regardless of locking, someone could be writing into the buffer */
1364 void systrace_print(bool all, struct proc *p)
1366 spin_lock_irqsave(&systrace_lock);
1367 /* if you want to be clever, you could make this start from the earliest
1368 * timestamp and loop around. Careful of concurrent writes. */
1369 for (int i = 0; i < systrace_bufsize; i++)
1370 if (systrace_buffer[i].timestamp)
1371 printk("[%16llu] Syscall %d for proc %d on core %d, vcore %d\n",
1372 systrace_buffer[i].timestamp,
1373 systrace_buffer[i].syscallno,
1374 systrace_buffer[i].pid,
1375 systrace_buffer[i].coreid,
1376 systrace_buffer[i].vcoreid);
1377 spin_unlock_irqsave(&systrace_lock);
1380 void systrace_clear_buffer(void)
1382 spin_lock_irqsave(&systrace_lock);
1383 memset(systrace_buffer, 0, sizeof(struct systrace_record)*MAX_NUM_TRACED);
1384 spin_unlock_irqsave(&systrace_lock);
1387 void set_retval(uint32_t retval)
1389 struct per_cpu_info* coreinfo = &per_cpu_info[core_id()];
1390 *(coreinfo->cur_ret.returnloc) = retval;
1392 void set_errno(uint32_t errno)
1394 struct per_cpu_info* coreinfo = &per_cpu_info[core_id()];
1395 if (coreinfo && coreinfo->cur_ret.errno_loc)
1396 *(coreinfo->cur_ret.errno_loc) = errno;