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 mode &= ~p->fs_env.umask;
838 file = do_file_open(t_path, oflag, mode);
839 user_memdup_free(p, t_path);
842 fd = insert_file(&p->open_files, file); /* stores the ref to file */
843 kref_put(&file->f_kref);
845 warn("File insertion failed");
848 printd("File %s Open, res=%d\n", path, fd);
852 static intreg_t sys_close(struct proc *p, int fd)
854 struct file *file = put_file_from_fd(&p->open_files, fd);
862 /* kept around til we remove the last ufe */
863 #define ufe(which,a0,a1,a2,a3) \
864 frontend_syscall_errno(p,APPSERVER_SYSCALL_##which,\
865 (int)(a0),(int)(a1),(int)(a2),(int)(a3))
867 static intreg_t sys_fstat(struct proc *p, int fd, struct kstat *u_stat)
870 struct file *file = get_file_from_fd(&p->open_files, fd);
875 kbuf = kmalloc(sizeof(struct kstat), 0);
877 kref_put(&file->f_kref);
881 stat_inode(file->f_dentry->d_inode, kbuf);
882 kref_put(&file->f_kref);
883 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
884 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
893 /* sys_stat() and sys_lstat() do nearly the same thing, differing in how they
894 * treat a symlink for the final item, which (probably) will be controlled by
895 * the lookup flags */
896 static intreg_t stat_helper(struct proc *p, const char *path, size_t path_l,
897 struct kstat *u_stat, int flags)
900 struct dentry *path_d;
901 char *t_path = user_strdup_errno(p, path, path_l);
904 path_d = lookup_dentry(t_path, flags);
905 user_memdup_free(p, t_path);
908 kbuf = kmalloc(sizeof(struct kstat), 0);
911 kref_put(&path_d->d_kref);
914 stat_inode(path_d->d_inode, kbuf);
915 kref_put(&path_d->d_kref);
916 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
917 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
926 /* Follow a final symlink */
927 static intreg_t sys_stat(struct proc *p, const char *path, size_t path_l,
928 struct kstat *u_stat)
930 return stat_helper(p, path, path_l, u_stat, LOOKUP_FOLLOW);
933 /* Don't follow a final symlink */
934 static intreg_t sys_lstat(struct proc *p, const char *path, size_t path_l,
935 struct kstat *u_stat)
937 return stat_helper(p, path, path_l, u_stat, 0);
940 intreg_t sys_fcntl(struct proc *p, int fd, int cmd, int arg)
943 struct file *file = get_file_from_fd(&p->open_files, fd);
950 printk("[kernel] dup not supported yet\n");
953 /* GET and SETFD just care about CLOEXEC. We don't have a separate
954 * flag variable for the FD (we might need to, technically). */
955 if (file->f_flags & O_CLOEXEC)
959 if (arg == FD_CLOEXEC)
960 file->f_flags |= O_CLOEXEC;
963 retval = file->f_flags;
966 /* only allowed to set certain flags. */
967 arg &= O_APPEND | O_ASYNC | O_DIRECT | O_NOATIME | O_NONBLOCK;
970 warn("Unsupported fcntl cmd %d\n", cmd);
972 kref_put(&file->f_kref);
976 static intreg_t sys_access(struct proc *p, const char *path, size_t path_l,
980 char *t_path = user_strdup_errno(p, path, path_l);
983 retval = do_access(t_path, mode);
984 user_memdup_free(p, t_path);
985 printd("Access for path: %s retval: %d\n", path, retval);
993 intreg_t sys_umask(struct proc *p, int mask)
995 int old_mask = p->fs_env.umask;
996 p->fs_env.umask = mask & 0777;
1000 intreg_t sys_chmod(struct proc *p, const char *path, size_t path_l, int mode)
1003 char *t_path = user_strdup_errno(p, path, path_l);
1006 retval = do_chmod(t_path, mode);
1007 user_memdup_free(p, t_path);
1015 static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
1018 struct file *file = get_file_from_fd(&p->open_files, fd);
1023 ret = file->f_op->llseek(file, offset, whence);
1024 kref_put(&file->f_kref);
1028 intreg_t sys_link(struct proc *p, char *old_path, size_t old_l,
1029 char *new_path, size_t new_l)
1032 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1033 if (t_oldpath == NULL)
1035 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1036 if (t_newpath == NULL) {
1037 user_memdup_free(p, t_oldpath);
1040 ret = do_link(t_oldpath, t_newpath);
1041 user_memdup_free(p, t_oldpath);
1042 user_memdup_free(p, t_newpath);
1046 intreg_t sys_unlink(struct proc *p, const char *path, size_t path_l)
1049 char *t_path = user_strdup_errno(p, path, path_l);
1052 retval = do_unlink(t_path);
1053 user_memdup_free(p, t_path);
1057 intreg_t sys_symlink(struct proc *p, char *old_path, size_t old_l,
1058 char *new_path, size_t new_l)
1061 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1062 if (t_oldpath == NULL)
1064 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1065 if (t_newpath == NULL) {
1066 user_memdup_free(p, t_oldpath);
1069 ret = do_symlink(new_path, old_path, S_IRWXU | S_IRWXG | S_IRWXO);
1070 user_memdup_free(p, t_oldpath);
1071 user_memdup_free(p, t_newpath);
1075 intreg_t sys_readlink(struct proc *p, char *path, size_t path_l,
1076 char *u_buf, size_t buf_l)
1080 struct dentry *path_d;
1081 char *t_path = user_strdup_errno(p, path, path_l);
1084 path_d = lookup_dentry(t_path, 0);
1085 user_memdup_free(p, t_path);
1088 symname = path_d->d_inode->i_op->readlink(path_d);
1089 copy_amt = strnlen(symname, buf_l - 1) + 1;
1090 if (memcpy_to_user_errno(p, u_buf, symname, copy_amt)) {
1091 kref_put(&path_d->d_kref);
1095 kref_put(&path_d->d_kref);
1096 printd("READLINK returning %s\n", u_buf);
1100 intreg_t sys_chdir(struct proc *p, const char *path, size_t path_l)
1103 char *t_path = user_strdup_errno(p, path, path_l);
1106 retval = do_chdir(&p->fs_env, t_path);
1107 user_memdup_free(p, t_path);
1115 /* Note cwd_l is not a strlen, it's an absolute size */
1116 intreg_t sys_getcwd(struct proc *p, char *u_cwd, size_t cwd_l)
1120 char *k_cwd = do_getcwd(&p->fs_env, &kfree_this, cwd_l);
1122 return -1; /* errno set by do_getcwd */
1123 if (memcpy_to_user_errno(p, u_cwd, k_cwd, strnlen(k_cwd, cwd_l - 1) + 1))
1129 intreg_t sys_mkdir(struct proc *p, const char *path, size_t path_l, int mode)
1132 char *t_path = user_strdup_errno(p, path, path_l);
1135 mode &= ~p->fs_env.umask;
1136 retval = do_mkdir(t_path, mode);
1137 user_memdup_free(p, t_path);
1141 intreg_t sys_rmdir(struct proc *p, const char *path, size_t path_l)
1144 char *t_path = user_strdup_errno(p, path, path_l);
1147 retval = do_rmdir(t_path);
1148 user_memdup_free(p, t_path);
1152 intreg_t sys_gettimeofday(struct proc *p, int *buf)
1154 static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
1157 spin_lock(>od_lock);
1160 #if (defined __CONFIG_APPSERVER__)
1161 t0 = ufe(time,0,0,0,0);
1163 // Nanwan's birthday, bitches!!
1166 spin_unlock(>od_lock);
1168 long long dt = read_tsc();
1169 int kbuf[2] = {t0+dt/system_timing.tsc_freq,
1170 (dt%system_timing.tsc_freq)*1000000/system_timing.tsc_freq};
1172 return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
1175 #define SIZEOF_STRUCT_TERMIOS 60
1176 intreg_t sys_tcgetattr(struct proc *p, int fd, void *termios_p)
1178 int* kbuf = kmalloc(SIZEOF_STRUCT_TERMIOS,0);
1179 int ret = ufe(tcgetattr,fd,PADDR(kbuf),0,0);
1180 if(ret != -1 && memcpy_to_user_errno(p,termios_p,kbuf,SIZEOF_STRUCT_TERMIOS))
1186 intreg_t sys_tcsetattr(struct proc *p, int fd, int optional_actions,
1187 const void *termios_p)
1189 void* kbuf = user_memdup_errno(p,termios_p,SIZEOF_STRUCT_TERMIOS);
1192 int ret = ufe(tcsetattr,fd,optional_actions,PADDR(kbuf),0);
1193 user_memdup_free(p,kbuf);
1197 /* TODO: we don't have any notion of UIDs or GIDs yet, but don't let that stop a
1198 * process from thinking it can do these. The other alternative is to have
1199 * glibc return 0 right away, though someone might want to do something with
1200 * these calls. Someday. */
1201 intreg_t sys_setuid(struct proc *p, uid_t uid)
1206 intreg_t sys_setgid(struct proc *p, gid_t gid)
1211 /************** Syscall Invokation **************/
1213 /* Executes the given syscall.
1215 * Note tf is passed in, which points to the tf of the context on the kernel
1216 * stack. If any syscall needs to block, it needs to save this info, as well as
1219 * This syscall function is used by both local syscall and arsc, and should
1220 * remain oblivious of the caller. */
1221 intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
1222 uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
1224 /* Initialize the return value and error code returned to 0 */
1225 set_retval(ESUCCESS);
1226 set_errno(ESUCCESS);
1228 typedef intreg_t (*syscall_t)(struct proc*,uintreg_t,uintreg_t,
1229 uintreg_t,uintreg_t,uintreg_t);
1231 const static syscall_t syscall_table[] = {
1232 [SYS_null] = (syscall_t)sys_null,
1233 [SYS_cache_buster] = (syscall_t)sys_cache_buster,
1234 [SYS_cache_invalidate] = (syscall_t)sys_cache_invalidate,
1235 [SYS_reboot] = (syscall_t)reboot,
1236 [SYS_cputs] = (syscall_t)sys_cputs,
1237 [SYS_cgetc] = (syscall_t)sys_cgetc,
1238 [SYS_getcpuid] = (syscall_t)sys_getcpuid,
1239 [SYS_getvcoreid] = (syscall_t)sys_getvcoreid,
1240 [SYS_getpid] = (syscall_t)sys_getpid,
1241 [SYS_proc_create] = (syscall_t)sys_proc_create,
1242 [SYS_proc_run] = (syscall_t)sys_proc_run,
1243 [SYS_proc_destroy] = (syscall_t)sys_proc_destroy,
1244 [SYS_yield] = (syscall_t)sys_proc_yield,
1245 [SYS_fork] = (syscall_t)sys_fork,
1246 [SYS_exec] = (syscall_t)sys_exec,
1247 [SYS_trywait] = (syscall_t)sys_trywait,
1248 [SYS_mmap] = (syscall_t)sys_mmap,
1249 [SYS_munmap] = (syscall_t)sys_munmap,
1250 [SYS_mprotect] = (syscall_t)sys_mprotect,
1251 [SYS_shared_page_alloc] = (syscall_t)sys_shared_page_alloc,
1252 [SYS_shared_page_free] = (syscall_t)sys_shared_page_free,
1253 [SYS_resource_req] = (syscall_t)resource_req,
1254 [SYS_notify] = (syscall_t)sys_notify,
1255 [SYS_self_notify] = (syscall_t)sys_self_notify,
1256 [SYS_halt_core] = (syscall_t)sys_halt_core,
1257 #ifdef __CONFIG_SERIAL_IO__
1258 [SYS_serial_read] = (syscall_t)sys_serial_read,
1259 [SYS_serial_write] = (syscall_t)sys_serial_write,
1261 #ifdef __CONFIG_NETWORKING__
1262 [SYS_eth_read] = (syscall_t)sys_eth_read,
1263 [SYS_eth_write] = (syscall_t)sys_eth_write,
1264 [SYS_eth_get_mac_addr] = (syscall_t)sys_eth_get_mac_addr,
1265 [SYS_eth_recv_check] = (syscall_t)sys_eth_recv_check,
1267 #ifdef __CONFIG_ARSC_SERVER__
1268 [SYS_init_arsc] = (syscall_t)sys_init_arsc,
1270 // Syscalls serviced by the appserver for now.
1271 [SYS_read] = (syscall_t)sys_read,
1272 [SYS_write] = (syscall_t)sys_write,
1273 [SYS_open] = (syscall_t)sys_open,
1274 [SYS_close] = (syscall_t)sys_close,
1275 [SYS_fstat] = (syscall_t)sys_fstat,
1276 [SYS_stat] = (syscall_t)sys_stat,
1277 [SYS_lstat] = (syscall_t)sys_lstat,
1278 [SYS_fcntl] = (syscall_t)sys_fcntl,
1279 [SYS_access] = (syscall_t)sys_access,
1280 [SYS_umask] = (syscall_t)sys_umask,
1281 [SYS_chmod] = (syscall_t)sys_chmod,
1282 [SYS_lseek] = (syscall_t)sys_lseek,
1283 [SYS_link] = (syscall_t)sys_link,
1284 [SYS_unlink] = (syscall_t)sys_unlink,
1285 [SYS_symlink] = (syscall_t)sys_symlink,
1286 [SYS_readlink] = (syscall_t)sys_readlink,
1287 [SYS_chdir] = (syscall_t)sys_chdir,
1288 [SYS_getcwd] = (syscall_t)sys_getcwd,
1289 [SYS_mkdir] = (syscall_t)sys_mkdir,
1290 [SYS_rmdir] = (syscall_t)sys_rmdir,
1291 [SYS_gettimeofday] = (syscall_t)sys_gettimeofday,
1292 [SYS_tcgetattr] = (syscall_t)sys_tcgetattr,
1293 [SYS_tcsetattr] = (syscall_t)sys_tcsetattr,
1294 [SYS_setuid] = (syscall_t)sys_setuid,
1295 [SYS_setgid] = (syscall_t)sys_setgid
1298 const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
1300 uint32_t coreid, vcoreid;
1301 if (systrace_flags & SYSTRACE_ON) {
1302 if ((systrace_flags & SYSTRACE_ALLPROC) || (proc_is_traced(p))) {
1304 vcoreid = proc_get_vcoreid(p, core_id());
1305 if (systrace_flags & SYSTRACE_LOUD) {
1306 printk("[%16llu] Syscall %d for proc %d on core %d, vcore %d\n",
1307 read_tsc(), syscallno, p->pid, coreid, vcoreid);
1309 struct systrace_record *trace;
1310 unsigned int idx, new_idx;
1312 idx = systrace_bufidx;
1313 new_idx = (idx + 1) % systrace_bufsize;
1314 } while (!atomic_comp_swap(&systrace_bufidx, idx, new_idx));
1315 trace = &systrace_buffer[idx];
1316 trace->timestamp = read_tsc();
1317 trace->syscallno = syscallno;
1318 trace->pid = p->pid;
1319 trace->coreid = coreid;
1320 trace->vcoreid = vcoreid;
1324 //printk("Incoming syscall on core: %d number: %d\n a1: %x\n "
1325 // " a2: %x\n a3: %x\n a4: %x\n a5: %x\n", core_id(),
1326 // syscallno, a1, a2, a3, a4, a5);
1328 if(syscallno > max_syscall || syscall_table[syscallno] == NULL)
1329 panic("Invalid syscall number %d for proc %x!", syscallno, *p);
1331 return syscall_table[syscallno](p,a1,a2,a3,a4,a5);
1334 /* Syscall tracing */
1335 static void __init_systrace(void)
1337 systrace_buffer = kmalloc(MAX_SYSTRACES*sizeof(struct systrace_record), 0);
1338 if (!systrace_buffer)
1339 panic("Unable to alloc a trace buffer\n");
1340 systrace_bufidx = 0;
1341 systrace_bufsize = MAX_SYSTRACES;
1342 /* Note we never free the buffer - it's around forever. Feel free to change
1343 * this if you want to change the size or something dynamically. */
1346 /* If you call this while it is running, it will change the mode */
1347 void systrace_start(bool silent)
1349 static bool init = FALSE;
1350 spin_lock_irqsave(&systrace_lock);
1355 systrace_flags = silent ? SYSTRACE_ON : SYSTRACE_ON | SYSTRACE_LOUD;
1356 spin_unlock_irqsave(&systrace_lock);
1359 int systrace_reg(bool all, struct proc *p)
1362 spin_lock_irqsave(&systrace_lock);
1364 printk("Tracing syscalls for all processes\n");
1365 systrace_flags |= SYSTRACE_ALLPROC;
1368 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1369 if (!systrace_procs[i]) {
1370 printk("Tracing syscalls for process %d\n", p->pid);
1371 systrace_procs[i] = p;
1377 spin_unlock_irqsave(&systrace_lock);
1381 void systrace_stop(void)
1383 spin_lock_irqsave(&systrace_lock);
1385 for (int i = 0; i < MAX_NUM_TRACED; i++)
1386 systrace_procs[i] = 0;
1387 spin_unlock_irqsave(&systrace_lock);
1390 /* If you registered a process specifically, then you need to dereg it
1391 * specifically. Or just fully stop, which will do it for all. */
1392 int systrace_dereg(bool all, struct proc *p)
1394 spin_lock_irqsave(&systrace_lock);
1396 printk("No longer tracing syscalls for all processes.\n");
1397 systrace_flags &= ~SYSTRACE_ALLPROC;
1399 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1400 if (systrace_procs[i] == p) {
1401 systrace_procs[i] = 0;
1402 printk("No longer tracing syscalls for process %d\n", p->pid);
1406 spin_unlock_irqsave(&systrace_lock);
1410 /* Regardless of locking, someone could be writing into the buffer */
1411 void systrace_print(bool all, struct proc *p)
1413 spin_lock_irqsave(&systrace_lock);
1414 /* if you want to be clever, you could make this start from the earliest
1415 * timestamp and loop around. Careful of concurrent writes. */
1416 for (int i = 0; i < systrace_bufsize; i++)
1417 if (systrace_buffer[i].timestamp)
1418 printk("[%16llu] Syscall %d for proc %d on core %d, vcore %d\n",
1419 systrace_buffer[i].timestamp,
1420 systrace_buffer[i].syscallno,
1421 systrace_buffer[i].pid,
1422 systrace_buffer[i].coreid,
1423 systrace_buffer[i].vcoreid);
1424 spin_unlock_irqsave(&systrace_lock);
1427 void systrace_clear_buffer(void)
1429 spin_lock_irqsave(&systrace_lock);
1430 memset(systrace_buffer, 0, sizeof(struct systrace_record)*MAX_NUM_TRACED);
1431 spin_unlock_irqsave(&systrace_lock);
1434 void set_retval(uint32_t retval)
1436 struct per_cpu_info* coreinfo = &per_cpu_info[core_id()];
1437 *(coreinfo->cur_ret.returnloc) = retval;
1439 void set_errno(uint32_t errno)
1441 struct per_cpu_info* coreinfo = &per_cpu_info[core_id()];
1442 if (coreinfo && coreinfo->cur_ret.errno_loc)
1443 *(coreinfo->cur_ret.errno_loc) = errno;