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 /************** Utility Syscalls **************/
64 static int sys_null(void)
69 // Writes 'val' to 'num_writes' entries of the well-known array in the kernel
70 // address space. It's just #defined to be some random 4MB chunk (which ought
71 // to be boot_alloced or something). Meant to grab exclusive access to cache
72 // lines, to simulate doing something useful.
73 static int sys_cache_buster(struct proc *p, uint32_t num_writes,
74 uint32_t num_pages, uint32_t flags)
75 { TRUSTEDBLOCK /* zra: this is not really part of the kernel */
76 #define BUSTER_ADDR 0xd0000000 // around 512 MB deep
77 #define MAX_WRITES 1048576*8
79 #define INSERT_ADDR (UINFO + 2*PGSIZE) // should be free for these tests
80 uint32_t* buster = (uint32_t*)BUSTER_ADDR;
81 static spinlock_t buster_lock = SPINLOCK_INITIALIZER;
83 page_t* a_page[MAX_PAGES];
85 /* Strided Accesses or Not (adjust to step by cachelines) */
87 if (flags & BUSTER_STRIDED) {
92 /* Shared Accesses or Not (adjust to use per-core regions)
93 * Careful, since this gives 8MB to each core, starting around 512MB.
94 * Also, doesn't separate memory for core 0 if it's an async call.
96 if (!(flags & BUSTER_SHARED))
97 buster = (uint32_t*)(BUSTER_ADDR + core_id() * 0x00800000);
99 /* Start the timer, if we're asked to print this info*/
100 if (flags & BUSTER_PRINT_TICKS)
101 ticks = start_timing();
103 /* Allocate num_pages (up to MAX_PAGES), to simulate doing some more
104 * realistic work. Note we don't write to these pages, even if we pick
105 * unshared. Mostly due to the inconvenience of having to match up the
106 * number of pages with the number of writes. And it's unnecessary.
109 spin_lock(&buster_lock);
110 for (int i = 0; i < MIN(num_pages, MAX_PAGES); i++) {
111 upage_alloc(p, &a_page[i],1);
112 page_insert(p->env_pgdir, a_page[i], (void*)INSERT_ADDR + PGSIZE*i,
114 page_decref(a_page[i]);
116 spin_unlock(&buster_lock);
119 if (flags & BUSTER_LOCKED)
120 spin_lock(&buster_lock);
121 for (int i = 0; i < MIN(num_writes, MAX_WRITES); i=i+stride)
122 buster[i] = 0xdeadbeef;
123 if (flags & BUSTER_LOCKED)
124 spin_unlock(&buster_lock);
127 spin_lock(&buster_lock);
128 for (int i = 0; i < MIN(num_pages, MAX_PAGES); i++) {
129 page_remove(p->env_pgdir, (void*)(INSERT_ADDR + PGSIZE * i));
130 page_decref(a_page[i]);
132 spin_unlock(&buster_lock);
136 if (flags & BUSTER_PRINT_TICKS) {
137 ticks = stop_timing(ticks);
138 printk("%llu,", ticks);
143 static int sys_cache_invalidate(void)
151 /* sys_reboot(): called directly from dispatch table. */
153 /* Print a string to the system console. */
154 static ssize_t sys_cputs(struct proc *p, const char *DANGEROUS string,
158 t_string = user_strdup_errno(p, string, strlen);
161 printk("%.*s", strlen, t_string);
162 user_memdup_free(p, t_string);
163 return (ssize_t)strlen;
166 // Read a character from the system console.
167 // Returns the character.
168 static uint16_t sys_cgetc(struct proc *p)
172 // The cons_getc() primitive doesn't wait for a character,
173 // but the sys_cgetc() system call does.
174 while ((c = cons_getc()) == 0)
180 /* Returns the id of the cpu this syscall is executed on. */
181 static uint32_t sys_getcpuid(void)
186 // TODO: Temporary hack until thread-local storage is implemented on i386 and
187 // this is removed from the user interface
188 static size_t sys_getvcoreid(struct proc *p)
190 return proc_get_vcoreid(p, core_id());
193 /************** Process management syscalls **************/
195 /* Returns the calling process's pid */
196 static pid_t sys_getpid(struct proc *p)
201 /* Creates a process from the file 'path'. The process is not runnable by
202 * default, so it needs it's status to be changed so that the next call to
203 * schedule() will try to run it. TODO: take args/envs from userspace. */
204 static int sys_proc_create(struct proc *p, char *path, size_t path_l,
209 struct file *program;
212 /* Copy in the path. Consider putting an upper bound on path_l. */
213 t_path = user_strdup_errno(p, path, path_l);
216 program = do_file_open(t_path, 0, 0);
217 user_memdup_free(p, t_path);
219 return -1; /* presumably, errno is already set */
220 /* TODO: need to split the proc creation, since you must load after setting
221 * args/env, since auxp gets set up there. */
222 //new_p = proc_create(program, 0, 0);
223 if (proc_alloc(&new_p, current))
225 /* Set the argument stuff needed by glibc */
226 if (memcpy_from_user_errno(p, new_p->procinfo->argp, pi->argp,
229 if (memcpy_from_user_errno(p, new_p->procinfo->argbuf, pi->argbuf,
232 if (load_elf(new_p, program))
234 kref_put(&program->f_kref);
235 /* Connect to stdin, stdout, stderr (part of proc_create()) */
236 assert(insert_file(&new_p->open_files, dev_stdin, 0) == 0);
237 assert(insert_file(&new_p->open_files, dev_stdout, 0) == 1);
238 assert(insert_file(&new_p->open_files, dev_stderr, 0) == 2);
241 kref_put(&new_p->kref); /* give up the reference created in proc_create() */
246 kref_put(&program->f_kref);
250 /* Makes process PID runnable. Consider moving the functionality to process.c */
251 static error_t sys_proc_run(struct proc *p, unsigned pid)
253 struct proc *target = pid2proc(pid);
258 // note we can get interrupted here. it's not bad.
259 spin_lock(&p->proc_lock);
260 // make sure we have access and it's in the right state to be activated
261 if (!proc_controls(p, target)) {
262 kref_put(&target->kref);
264 } else if (target->state != PROC_CREATED) {
265 kref_put(&target->kref);
268 __proc_set_state(target, PROC_RUNNABLE_S);
269 schedule_proc(target);
271 spin_unlock(&p->proc_lock);
272 kref_put(&target->kref);
276 /* Destroy proc pid. If this is called by the dying process, it will never
277 * return. o/w it will return 0 on success, or an error. Errors include:
278 * - EBADPROC: if there is no such process with pid
279 * - EPERM: if caller does not control pid */
280 static error_t sys_proc_destroy(struct proc *p, pid_t pid, int exitcode)
283 struct proc *p_to_die = pid2proc(pid);
289 if (!proc_controls(p, p_to_die)) {
290 kref_put(&p_to_die->kref);
295 // syscall code and pid2proc both have edible references, only need 1.
296 p->exitcode = exitcode;
297 kref_put(&p_to_die->kref);
298 printd("[PID %d] proc exiting gracefully (code %d)\n", p->pid,exitcode);
300 printd("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
302 proc_destroy(p_to_die);
303 kref_put(&p_to_die->kref);
307 static int sys_proc_yield(struct proc *p, bool being_nice)
309 proc_yield(p, being_nice);
313 static ssize_t sys_fork(env_t* e)
315 // TODO: right now we only support fork for single-core processes
316 if (e->state != PROC_RUNNING_S) {
320 /* Can't really fork if we don't have a current_tf to fork */
326 assert(!proc_alloc(&env, current));
329 env->heap_top = e->heap_top;
331 env->env_tf = *current_tf;
333 env->cache_colors_map = cache_colors_map_alloc();
334 for(int i=0; i < llc_cache->num_colors; i++)
335 if(GET_BITMASK_BIT(e->cache_colors_map,i))
336 cache_color_alloc(llc_cache, env->cache_colors_map);
338 duplicate_vmrs(e, env);
340 int copy_page(env_t* e, pte_t* pte, void* va, void* arg)
342 env_t* env = (env_t*)arg;
344 if(PAGE_PRESENT(*pte))
347 if(upage_alloc(env,&pp,0))
349 if(page_insert(env->env_pgdir,pp,va,*pte & PTE_PERM))
354 pagecopy(page2kva(pp),ppn2kva(PTE2PPN(*pte)));
357 assert(PAGE_PAGED_OUT(*pte));
358 /* TODO: (SWAP) will need to either make a copy or CoW/refcnt the
359 * backend store. For now, this PTE will be the same as the
361 panic("Swapping not supported!");
362 pte_t* newpte = pgdir_walk(env->env_pgdir,va,1);
370 // TODO: (PC) this won't work. Needs revisiting.
371 // copy procdata and procinfo
372 memcpy(env->procdata,e->procdata,sizeof(struct procdata));
373 memcpy(env->procinfo,e->procinfo,sizeof(struct procinfo));
374 env->procinfo->pid = env->pid;
375 env->procinfo->ppid = env->ppid;
377 /* for now, just copy the contents of every present page in the entire
379 if (env_user_mem_walk(e, 0, UMAPTOP, ©_page, env)) {
380 proc_destroy(env); /* this is prob what you want, not decref by 2 */
384 clone_files(&e->open_files, &env->open_files);
386 __proc_set_state(env, PROC_RUNNABLE_S);
389 // don't decref the new process.
390 // that will happen when the parent waits for it.
391 // TODO: if the parent doesn't wait, we need to change the child's parent
392 // when the parent dies, or at least decref it
394 printd("[PID %d] fork PID %d\n",e->pid,env->pid);
398 /* Load the binary "path" into the current process, and start executing it.
399 * argv and envp are magically bundled in procinfo for now. Keep in sync with
400 * glibc's sysdeps/ros/execve.c */
401 static int sys_exec(struct proc *p, char *path, size_t path_l,
406 struct file *program;
408 /* We probably want it to never be allowed to exec if it ever was _M */
409 if (p->state != PROC_RUNNING_S) {
413 /* Can't really exec if we don't have a current_tf to reset */
418 /* Copy in the path. Consider putting an upper bound on path_l. */
419 t_path = user_strdup_errno(p, path, path_l);
422 program = do_file_open(t_path, 0, 0);
423 user_memdup_free(p, t_path);
425 return -1; /* presumably, errno is already set */
426 /* Set the argument stuff needed by glibc */
427 if (memcpy_from_user_errno(p, p->procinfo->argp, pi->argp,
430 if (memcpy_from_user_errno(p, p->procinfo->argbuf, pi->argbuf,
433 /* This is the point of no return for the process. */
434 /* TODO: issues with this: Need to also assert there are no outstanding
435 * users of the sysrings. the ldt page will get freed shortly, so that's
436 * okay. Potentially issues with the nm and vcpd if we were in _M before
437 * and someone is trying to notify. */
438 memset(p->procdata, 0, sizeof(procdata_t));
440 close_all_files(&p->open_files, TRUE);
441 env_user_mem_free(p, 0, UMAPTOP);
442 if (load_elf(p, program)) {
443 kref_put(&program->f_kref);
445 smp_idle(); /* syscall can't return on failure now */
447 printd("[PID %d] exec %s\n", p->pid, file_name(program));
448 kref_put(&program->f_kref);
449 *current_tf = p->env_tf;
452 kref_put(&program->f_kref);
456 static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
458 struct proc* p = pid2proc(pid);
460 // TODO: this syscall is racy, so we only support for single-core procs
461 if(e->state != PROC_RUNNING_S)
464 // TODO: need to use errno properly. sadly, ROS error codes conflict..
470 if(current->pid == p->ppid)
472 if(p->state == PROC_DYING)
474 memcpy_to_user(e,status,&p->exitcode,sizeof(int));
475 printd("[PID %d] waited for PID %d (code %d)\n",
476 e->pid,p->pid,p->exitcode);
485 else // not a child of the calling process
491 // if the wait succeeded, decref twice
502 /************** Memory Management Syscalls **************/
504 static void *sys_mmap(struct proc *p, uintreg_t a1, uintreg_t a2, uintreg_t a3,
508 if (memcpy_from_user(p, _a456, a456, 3 * sizeof(uintreg_t)))
509 sys_proc_destroy(p, p->pid, -1);
510 return mmap(p, a1, a2, a3, _a456[0], _a456[1], _a456[2]);
513 static intreg_t sys_mprotect(struct proc *p, void *addr, size_t len, int prot)
515 return mprotect(p, (uintptr_t)addr, len, prot);
518 static intreg_t sys_munmap(struct proc *p, void *addr, size_t len)
520 return munmap(p, (uintptr_t)addr, len);
523 static ssize_t sys_shared_page_alloc(env_t* p1,
524 void**DANGEROUS _addr, pid_t p2_id,
525 int p1_flags, int p2_flags
528 printk("[kernel] shared page alloc is deprecated/unimplemented.\n");
532 static int sys_shared_page_free(env_t* p1, void*DANGEROUS addr, pid_t p2)
538 /* sys_resource_req(): called directly from dispatch table. */
540 /* Will notify the target on the given vcore, if the caller controls the target.
541 * Will honor the target's wanted/vcoreid. u_ne can be NULL. */
542 static int sys_notify(struct proc *p, int target_pid, unsigned int notif,
543 struct notif_event *u_ne)
545 struct notif_event local_ne;
546 struct proc *target = pid2proc(target_pid);
552 if (!proc_controls(p, target)) {
553 kref_put(&target->kref);
557 /* if the user provided a notif_event, copy it in and use that */
559 if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
560 kref_put(&target->kref);
564 proc_notify(target, local_ne.ne_type, &local_ne);
566 proc_notify(target, notif, 0);
568 kref_put(&target->kref);
572 /* Will notify the calling process on the given vcore, independently of WANTED
573 * or advertised vcoreid. If you change the parameters, change pop_ros_tf() */
574 static int sys_self_notify(struct proc *p, uint32_t vcoreid, unsigned int notif,
575 struct notif_event *u_ne)
577 struct notif_event local_ne;
579 printd("[kernel] received self notify for vcoreid %d, notif %d, ne %08p\n",
580 vcoreid, notif, u_ne);
581 /* if the user provided a notif_event, copy it in and use that */
583 if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
587 do_notify(p, vcoreid, local_ne.ne_type, &local_ne);
589 do_notify(p, vcoreid, notif, 0);
594 /* This will set a local timer for usec, then shut down the core */
595 static int sys_halt_core(struct proc *p, unsigned int usec)
597 /* TODO: ought to check and see if a timer was already active, etc, esp so
598 * userspace can't turn off timers. also note we will also call whatever
599 * timer_interrupt() will do, though all we care about is just
600 * self_ipi/interrupting. */
601 set_core_timer(usec);
607 /************** Platform Specific Syscalls **************/
609 //Read a buffer over the serial port
610 static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
612 printk("[kernel] serial reading is deprecated.\n");
616 #ifdef __CONFIG_SERIAL_IO__
617 char *COUNT(len) buf = user_mem_assert(e, _buf, len, PTE_USER_RO);
618 size_t bytes_read = 0;
620 while((c = serial_read_byte()) != -1) {
621 buf[bytes_read++] = (uint8_t)c;
622 if(bytes_read == len) break;
624 return (ssize_t)bytes_read;
630 //Write a buffer over the serial port
631 static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
633 printk("[kernel] serial writing is deprecated.\n");
636 #ifdef __CONFIG_SERIAL_IO__
637 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_USER_RO);
638 for(int i =0; i<len; i++)
639 serial_send_byte(buf[i]);
646 #ifdef __CONFIG_NETWORKING__
647 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
648 static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
655 spin_lock(&packet_buffers_lock);
657 if (num_packet_buffers == 0) {
658 spin_unlock(&packet_buffers_lock);
662 ptr = packet_buffers[packet_buffers_head];
663 len = packet_buffers_sizes[packet_buffers_head];
665 num_packet_buffers--;
666 packet_buffers_head = (packet_buffers_head + 1) % MAX_PACKET_BUFFERS;
668 spin_unlock(&packet_buffers_lock);
670 char* _buf = user_mem_assert(e, buf, len, PTE_U);
672 memcpy(_buf, ptr, len);
682 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
683 static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
690 // HACK TO BYPASS HACK
691 int just_sent = send_frame(buf, len);
694 printk("Packet send fail\n");
700 // END OF RECURSIVE HACK
702 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_U);
705 int cur_packet_len = 0;
706 while (total_sent != len) {
707 cur_packet_len = ((len - total_sent) > MTU) ? MTU : (len - total_sent);
708 char dest_mac[6] = APPSERVER_MAC_ADDRESS;
709 char* wrap_buffer = eth_wrap(_buf + total_sent, cur_packet_len, device_mac, dest_mac, APPSERVER_PORT);
710 just_sent = send_frame(wrap_buffer, cur_packet_len + sizeof(struct ETH_Header));
713 return 0; // This should be an error code of its own
718 total_sent += cur_packet_len;
728 static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf)
731 for (int i = 0; i < 6; i++)
732 buf[i] = device_mac[i];
739 static int sys_eth_recv_check(env_t* e)
741 if (num_packet_buffers != 0)
749 static intreg_t sys_read(struct proc *p, int fd, void *buf, int len)
752 struct file *file = get_file_from_fd(&p->open_files, fd);
757 if (!file->f_op->read) {
758 kref_put(&file->f_kref);
762 /* TODO: (UMEM) currently, read() handles user memcpy issues, but we
763 * probably should user_mem_check and pin the region here, so read doesn't
765 ret = file->f_op->read(file, buf, len, &file->f_pos);
766 kref_put(&file->f_kref);
770 static intreg_t sys_write(struct proc *p, int fd, const void *buf, int len)
773 struct file *file = get_file_from_fd(&p->open_files, fd);
778 if (!file->f_op->write) {
779 kref_put(&file->f_kref);
784 ret = file->f_op->write(file, buf, len, &file->f_pos);
785 kref_put(&file->f_kref);
789 /* Checks args/reads in the path, opens the file, and inserts it into the
790 * process's open file list.
792 * TODO: take the path length */
793 static intreg_t sys_open(struct proc *p, const char *path, size_t path_l,
799 printd("File %s Open attempt\n", path);
800 char *t_path = user_strdup_errno(p, path, path_l);
803 mode &= ~p->fs_env.umask;
804 file = do_file_open(t_path, oflag, mode);
805 user_memdup_free(p, t_path);
808 fd = insert_file(&p->open_files, file, 0); /* stores the ref to file */
809 kref_put(&file->f_kref);
811 warn("File insertion failed");
814 printd("File %s Open, res=%d\n", path, fd);
818 static intreg_t sys_close(struct proc *p, int fd)
820 struct file *file = put_file_from_fd(&p->open_files, fd);
828 /* kept around til we remove the last ufe */
829 #define ufe(which,a0,a1,a2,a3) \
830 frontend_syscall_errno(p,APPSERVER_SYSCALL_##which,\
831 (int)(a0),(int)(a1),(int)(a2),(int)(a3))
833 static intreg_t sys_fstat(struct proc *p, int fd, struct kstat *u_stat)
836 struct file *file = get_file_from_fd(&p->open_files, fd);
841 kbuf = kmalloc(sizeof(struct kstat), 0);
843 kref_put(&file->f_kref);
847 stat_inode(file->f_dentry->d_inode, kbuf);
848 kref_put(&file->f_kref);
849 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
850 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
859 /* sys_stat() and sys_lstat() do nearly the same thing, differing in how they
860 * treat a symlink for the final item, which (probably) will be controlled by
861 * the lookup flags */
862 static intreg_t stat_helper(struct proc *p, const char *path, size_t path_l,
863 struct kstat *u_stat, int flags)
866 struct dentry *path_d;
867 char *t_path = user_strdup_errno(p, path, path_l);
870 path_d = lookup_dentry(t_path, flags);
871 user_memdup_free(p, t_path);
874 kbuf = kmalloc(sizeof(struct kstat), 0);
877 kref_put(&path_d->d_kref);
880 stat_inode(path_d->d_inode, kbuf);
881 kref_put(&path_d->d_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 /* Follow a final symlink */
893 static intreg_t sys_stat(struct proc *p, const char *path, size_t path_l,
894 struct kstat *u_stat)
896 return stat_helper(p, path, path_l, u_stat, LOOKUP_FOLLOW);
899 /* Don't follow a final symlink */
900 static intreg_t sys_lstat(struct proc *p, const char *path, size_t path_l,
901 struct kstat *u_stat)
903 return stat_helper(p, path, path_l, u_stat, 0);
906 intreg_t sys_fcntl(struct proc *p, int fd, int cmd, int arg)
909 struct file *file = get_file_from_fd(&p->open_files, fd);
916 retval = insert_file(&p->open_files, file, arg);
923 /* GET and SETFD just care about CLOEXEC. We don't have a separate
924 * flag variable for the FD (we might need to, technically). */
925 if (file->f_flags & O_CLOEXEC)
929 if (arg == FD_CLOEXEC)
930 file->f_flags |= O_CLOEXEC;
933 retval = file->f_flags;
936 /* only allowed to set certain flags. */
937 arg &= O_FCNTL_FLAGS;
938 file->f_flags = (file->f_flags & ~O_FCNTL_FLAGS) | arg;
941 warn("Unsupported fcntl cmd %d\n", cmd);
943 kref_put(&file->f_kref);
947 static intreg_t sys_access(struct proc *p, const char *path, size_t path_l,
951 char *t_path = user_strdup_errno(p, path, path_l);
954 retval = do_access(t_path, mode);
955 user_memdup_free(p, t_path);
956 printd("Access for path: %s retval: %d\n", path, retval);
964 intreg_t sys_umask(struct proc *p, int mask)
966 int old_mask = p->fs_env.umask;
967 p->fs_env.umask = mask & S_PMASK;
971 intreg_t sys_chmod(struct proc *p, const char *path, size_t path_l, int mode)
974 char *t_path = user_strdup_errno(p, path, path_l);
977 retval = do_chmod(t_path, mode);
978 user_memdup_free(p, t_path);
986 static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
989 struct file *file = get_file_from_fd(&p->open_files, fd);
994 ret = file->f_op->llseek(file, offset, whence);
995 kref_put(&file->f_kref);
999 intreg_t sys_link(struct proc *p, char *old_path, size_t old_l,
1000 char *new_path, size_t new_l)
1003 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1004 if (t_oldpath == NULL)
1006 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1007 if (t_newpath == NULL) {
1008 user_memdup_free(p, t_oldpath);
1011 ret = do_link(t_oldpath, t_newpath);
1012 user_memdup_free(p, t_oldpath);
1013 user_memdup_free(p, t_newpath);
1017 intreg_t sys_unlink(struct proc *p, const char *path, size_t path_l)
1020 char *t_path = user_strdup_errno(p, path, path_l);
1023 retval = do_unlink(t_path);
1024 user_memdup_free(p, t_path);
1028 intreg_t sys_symlink(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_symlink(new_path, old_path, S_IRWXU | S_IRWXG | S_IRWXO);
1041 user_memdup_free(p, t_oldpath);
1042 user_memdup_free(p, t_newpath);
1046 intreg_t sys_readlink(struct proc *p, char *path, size_t path_l,
1047 char *u_buf, size_t buf_l)
1051 struct dentry *path_d;
1052 char *t_path = user_strdup_errno(p, path, path_l);
1055 path_d = lookup_dentry(t_path, 0);
1056 user_memdup_free(p, t_path);
1059 symname = path_d->d_inode->i_op->readlink(path_d);
1060 copy_amt = strnlen(symname, buf_l - 1) + 1;
1061 if (memcpy_to_user_errno(p, u_buf, symname, copy_amt)) {
1062 kref_put(&path_d->d_kref);
1066 kref_put(&path_d->d_kref);
1067 printd("READLINK returning %s\n", u_buf);
1071 intreg_t sys_chdir(struct proc *p, const char *path, size_t path_l)
1074 char *t_path = user_strdup_errno(p, path, path_l);
1077 retval = do_chdir(&p->fs_env, t_path);
1078 user_memdup_free(p, t_path);
1086 /* Note cwd_l is not a strlen, it's an absolute size */
1087 intreg_t sys_getcwd(struct proc *p, char *u_cwd, size_t cwd_l)
1091 char *k_cwd = do_getcwd(&p->fs_env, &kfree_this, cwd_l);
1093 return -1; /* errno set by do_getcwd */
1094 if (memcpy_to_user_errno(p, u_cwd, k_cwd, strnlen(k_cwd, cwd_l - 1) + 1))
1100 intreg_t sys_mkdir(struct proc *p, const char *path, size_t path_l, int mode)
1103 char *t_path = user_strdup_errno(p, path, path_l);
1106 mode &= ~p->fs_env.umask;
1107 retval = do_mkdir(t_path, mode);
1108 user_memdup_free(p, t_path);
1112 intreg_t sys_rmdir(struct proc *p, const char *path, size_t path_l)
1115 char *t_path = user_strdup_errno(p, path, path_l);
1118 retval = do_rmdir(t_path);
1119 user_memdup_free(p, t_path);
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 /* TODO: we don't have any notion of UIDs or GIDs yet, but don't let that stop a
1169 * process from thinking it can do these. The other alternative is to have
1170 * glibc return 0 right away, though someone might want to do something with
1171 * these calls. Someday. */
1172 intreg_t sys_setuid(struct proc *p, uid_t uid)
1177 intreg_t sys_setgid(struct proc *p, gid_t gid)
1182 /************** Syscall Invokation **************/
1184 const static struct sys_table_entry syscall_table[] = {
1185 [SYS_null] = {(syscall_t)sys_null, "null"},
1186 [SYS_cache_buster] = {(syscall_t)sys_cache_buster, "buster"},
1187 [SYS_cache_invalidate] = {(syscall_t)sys_cache_invalidate, "wbinv"},
1188 [SYS_reboot] = {(syscall_t)reboot, "reboot!"},
1189 [SYS_cputs] = {(syscall_t)sys_cputs, "cputs"},
1190 [SYS_cgetc] = {(syscall_t)sys_cgetc, "cgetc"},
1191 [SYS_getcpuid] = {(syscall_t)sys_getcpuid, "getcpuid"},
1192 [SYS_getvcoreid] = {(syscall_t)sys_getvcoreid, "getvcoreid"},
1193 [SYS_getpid] = {(syscall_t)sys_getpid, "getpid"},
1194 [SYS_proc_create] = {(syscall_t)sys_proc_create, "proc_create"},
1195 [SYS_proc_run] = {(syscall_t)sys_proc_run, "proc_run"},
1196 [SYS_proc_destroy] = {(syscall_t)sys_proc_destroy, "proc_destroy"},
1197 [SYS_yield] = {(syscall_t)sys_proc_yield, "proc_yield"},
1198 [SYS_fork] = {(syscall_t)sys_fork, "fork"},
1199 [SYS_exec] = {(syscall_t)sys_exec, "exec"},
1200 [SYS_trywait] = {(syscall_t)sys_trywait, "trywait"},
1201 [SYS_mmap] = {(syscall_t)sys_mmap, "mmap"},
1202 [SYS_munmap] = {(syscall_t)sys_munmap, "munmap"},
1203 [SYS_mprotect] = {(syscall_t)sys_mprotect, "mprotect"},
1204 [SYS_shared_page_alloc] = {(syscall_t)sys_shared_page_alloc, "pa"},
1205 [SYS_shared_page_free] = {(syscall_t)sys_shared_page_free, "pf"},
1206 [SYS_resource_req] = {(syscall_t)resource_req, "resource_req"},
1207 [SYS_notify] = {(syscall_t)sys_notify, "notify"},
1208 [SYS_self_notify] = {(syscall_t)sys_self_notify, "self_notify"},
1209 [SYS_halt_core] = {(syscall_t)sys_halt_core, "halt_core"},
1210 #ifdef __CONFIG_SERIAL_IO__
1211 [SYS_serial_read] = {(syscall_t)sys_serial_read, "ser_read"},
1212 [SYS_serial_write] = {(syscall_t)sys_serial_write, "ser_write"},
1214 #ifdef __CONFIG_NETWORKING__
1215 [SYS_eth_read] = {(syscall_t)sys_eth_read, "eth_read"},
1216 [SYS_eth_write] = {(syscall_t)sys_eth_write, "eth_write"},
1217 [SYS_eth_get_mac_addr] = {(syscall_t)sys_eth_get_mac_addr, "get_mac"},
1218 [SYS_eth_recv_check] = {(syscall_t)sys_eth_recv_check, "recv_check"},
1220 #ifdef __CONFIG_ARSC_SERVER__
1221 [SYS_init_arsc] = {(syscall_t)sys_init_arsc, "init_arsc"},
1223 [SYS_read] = {(syscall_t)sys_read, "read"},
1224 [SYS_write] = {(syscall_t)sys_write, "write"},
1225 [SYS_open] = {(syscall_t)sys_open, "open"},
1226 [SYS_close] = {(syscall_t)sys_close, "close"},
1227 [SYS_fstat] = {(syscall_t)sys_fstat, "fstat"},
1228 [SYS_stat] = {(syscall_t)sys_stat, "stat"},
1229 [SYS_lstat] = {(syscall_t)sys_lstat, "lstat"},
1230 [SYS_fcntl] = {(syscall_t)sys_fcntl, "fcntl"},
1231 [SYS_access] = {(syscall_t)sys_access, "access"},
1232 [SYS_umask] = {(syscall_t)sys_umask, "umask"},
1233 [SYS_chmod] = {(syscall_t)sys_chmod, "chmod"},
1234 [SYS_lseek] = {(syscall_t)sys_lseek, "lseek"},
1235 [SYS_link] = {(syscall_t)sys_link, "link"},
1236 [SYS_unlink] = {(syscall_t)sys_unlink, "unlink"},
1237 [SYS_symlink] = {(syscall_t)sys_symlink, "symlink"},
1238 [SYS_readlink] = {(syscall_t)sys_readlink, "readlink"},
1239 [SYS_chdir] = {(syscall_t)sys_chdir, "chdir"},
1240 [SYS_getcwd] = {(syscall_t)sys_getcwd, "getcwd"},
1241 [SYS_mkdir] = {(syscall_t)sys_mkdir, "mkdri"},
1242 [SYS_rmdir] = {(syscall_t)sys_rmdir, "rmdir"},
1243 [SYS_gettimeofday] = {(syscall_t)sys_gettimeofday, "gettime"},
1244 [SYS_tcgetattr] = {(syscall_t)sys_tcgetattr, "tcgetattr"},
1245 [SYS_tcsetattr] = {(syscall_t)sys_tcsetattr, "tcsetattr"},
1246 [SYS_setuid] = {(syscall_t)sys_setuid, "setuid"},
1247 [SYS_setgid] = {(syscall_t)sys_setgid, "setgid"}
1250 /* Executes the given syscall.
1252 * Note tf is passed in, which points to the tf of the context on the kernel
1253 * stack. If any syscall needs to block, it needs to save this info, as well as
1256 * This syscall function is used by both local syscall and arsc, and should
1257 * remain oblivious of the caller. */
1258 intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
1259 uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
1261 /* Initialize the return value and error code returned to 0 */
1262 set_retval(ESUCCESS);
1263 set_errno(ESUCCESS);
1265 const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
1267 uint32_t coreid, vcoreid;
1268 if (systrace_flags & SYSTRACE_ON) {
1269 if ((systrace_flags & SYSTRACE_ALLPROC) || (proc_is_traced(p))) {
1271 vcoreid = proc_get_vcoreid(p, coreid);
1272 if (systrace_flags & SYSTRACE_LOUD) {
1273 printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, "
1274 "%08p) proc: %d core: %d vcore: %d\n", read_tsc(),
1275 syscallno, syscall_table[syscallno].name, a1, a2, a3,
1276 a4, a5, p->pid, coreid, vcoreid);
1278 struct systrace_record *trace;
1279 unsigned int idx, new_idx;
1281 idx = systrace_bufidx;
1282 new_idx = (idx + 1) % systrace_bufsize;
1283 } while (!atomic_comp_swap(&systrace_bufidx, idx, new_idx));
1284 trace = &systrace_buffer[idx];
1285 trace->timestamp = read_tsc();
1286 trace->syscallno = syscallno;
1292 trace->pid = p->pid;
1293 trace->coreid = coreid;
1294 trace->vcoreid = vcoreid;
1298 //printk("Incoming syscall on core: %d number: %d\n a1: %x\n "
1299 // " a2: %x\n a3: %x\n a4: %x\n a5: %x\n", core_id(),
1300 // syscallno, a1, a2, a3, a4, a5);
1302 if (syscallno > max_syscall || syscall_table[syscallno].call == NULL)
1303 panic("Invalid syscall number %d for proc %x!", syscallno, *p);
1305 return syscall_table[syscallno].call(p, a1, a2, a3, a4, a5);
1308 /* Syscall tracing */
1309 static void __init_systrace(void)
1311 systrace_buffer = kmalloc(MAX_SYSTRACES*sizeof(struct systrace_record), 0);
1312 if (!systrace_buffer)
1313 panic("Unable to alloc a trace buffer\n");
1314 systrace_bufidx = 0;
1315 systrace_bufsize = MAX_SYSTRACES;
1316 /* Note we never free the buffer - it's around forever. Feel free to change
1317 * this if you want to change the size or something dynamically. */
1320 /* If you call this while it is running, it will change the mode */
1321 void systrace_start(bool silent)
1323 static bool init = FALSE;
1324 spin_lock_irqsave(&systrace_lock);
1329 systrace_flags = silent ? SYSTRACE_ON : SYSTRACE_ON | SYSTRACE_LOUD;
1330 spin_unlock_irqsave(&systrace_lock);
1333 int systrace_reg(bool all, struct proc *p)
1336 spin_lock_irqsave(&systrace_lock);
1338 printk("Tracing syscalls for all processes\n");
1339 systrace_flags |= SYSTRACE_ALLPROC;
1342 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1343 if (!systrace_procs[i]) {
1344 printk("Tracing syscalls for process %d\n", p->pid);
1345 systrace_procs[i] = p;
1351 spin_unlock_irqsave(&systrace_lock);
1355 void systrace_stop(void)
1357 spin_lock_irqsave(&systrace_lock);
1359 for (int i = 0; i < MAX_NUM_TRACED; i++)
1360 systrace_procs[i] = 0;
1361 spin_unlock_irqsave(&systrace_lock);
1364 /* If you registered a process specifically, then you need to dereg it
1365 * specifically. Or just fully stop, which will do it for all. */
1366 int systrace_dereg(bool all, struct proc *p)
1368 spin_lock_irqsave(&systrace_lock);
1370 printk("No longer tracing syscalls for all processes.\n");
1371 systrace_flags &= ~SYSTRACE_ALLPROC;
1373 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1374 if (systrace_procs[i] == p) {
1375 systrace_procs[i] = 0;
1376 printk("No longer tracing syscalls for process %d\n", p->pid);
1380 spin_unlock_irqsave(&systrace_lock);
1384 /* Regardless of locking, someone could be writing into the buffer */
1385 void systrace_print(bool all, struct proc *p)
1387 spin_lock_irqsave(&systrace_lock);
1388 /* if you want to be clever, you could make this start from the earliest
1389 * timestamp and loop around. Careful of concurrent writes. */
1390 for (int i = 0; i < systrace_bufsize; i++)
1391 if (systrace_buffer[i].timestamp)
1392 printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, "
1393 "%08p) proc: %d core: %d vcore: %d\n",
1394 systrace_buffer[i].timestamp,
1395 systrace_buffer[i].syscallno,
1396 syscall_table[systrace_buffer[i].syscallno].name,
1397 systrace_buffer[i].arg1,
1398 systrace_buffer[i].arg2,
1399 systrace_buffer[i].arg3,
1400 systrace_buffer[i].arg4,
1401 systrace_buffer[i].arg5,
1402 systrace_buffer[i].pid,
1403 systrace_buffer[i].coreid,
1404 systrace_buffer[i].vcoreid);
1405 spin_unlock_irqsave(&systrace_lock);
1408 void systrace_clear_buffer(void)
1410 spin_lock_irqsave(&systrace_lock);
1411 memset(systrace_buffer, 0, sizeof(struct systrace_record) * MAX_SYSTRACES);
1412 spin_unlock_irqsave(&systrace_lock);
1415 void set_retval(uint32_t retval)
1417 struct per_cpu_info* coreinfo = &per_cpu_info[core_id()];
1418 *(coreinfo->cur_ret.returnloc) = retval;
1420 void set_errno(uint32_t errno)
1422 struct per_cpu_info* coreinfo = &per_cpu_info[core_id()];
1423 if (coreinfo && coreinfo->cur_ret.errno_loc)
1424 *(coreinfo->cur_ret.errno_loc) = errno;