1 /* See COPYRIGHT for copyright information. */
7 #include <ros/common.h>
8 #include <arch/types.h>
11 #include <arch/console.h>
28 #include <colored_caches.h>
29 #include <hashtable.h>
34 #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 uint32_t systrace_bufidx = 0;
49 size_t systrace_bufsize = 0;
50 struct proc *systrace_procs[MAX_NUM_TRACED] = {0};
51 spinlock_t systrace_lock = SPINLOCK_INITIALIZER;
53 /* Not enforcing the packing of systrace_procs yet, but don't rely on that */
54 static bool proc_is_traced(struct proc *p)
56 for (int i = 0; i < MAX_NUM_TRACED; i++)
57 if (systrace_procs[i] == p)
62 /* Helper to finish a syscall, signalling if appropriate */
63 static void finish_sysc(struct syscall *sysc, struct proc *p)
65 /* Atomically turn on the LOCK and SC_DONE flag. The lock tells userspace
66 * we're messing with the flags and to not proceed. We use it instead of
67 * CASing with userspace. We need the atomics since we're racing with
68 * userspace for the event_queue registration. The 'lock' tells userspace
69 * to not muck with the flags while we're signalling. */
70 atomic_or(&sysc->flags, SC_K_LOCK | SC_DONE);
71 __signal_syscall(sysc, p);
72 atomic_and(&sysc->flags, ~SC_K_LOCK);
75 /* Helper that "finishes" the current async syscall. This should be used with
76 * care when we are not using the normal syscall completion path.
78 * Do *NOT* complete the same syscall twice. This is catastrophic for _Ms, and
81 * It is possible for another user thread to see the syscall being done early -
82 * they just need to be careful with the weird proc management calls (as in,
83 * don't trust an async fork).
85 * *sysc is in user memory, and should be pinned (TODO: UMEM). There may be
86 * issues with unpinning this if we never return. */
87 static void finish_current_sysc(int retval)
89 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
90 assert(pcpui->cur_sysc);
91 pcpui->cur_sysc->retval = retval;
92 finish_sysc(pcpui->cur_sysc, pcpui->cur_proc);
95 /* Callable by any function while executing a syscall (or otherwise, actually).
97 void set_errno(int errno)
99 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
101 pcpui->cur_sysc->err = errno;
104 /************** Utility Syscalls **************/
106 static int sys_null(void)
111 /* Diagnostic function: blocks the kthread/syscall, to help userspace test its
112 * async I/O handling. */
113 static int sys_block(struct proc *p, unsigned int usec)
115 struct timer_chain *tchain = &per_cpu_info[core_id()].tchain;
116 struct alarm_waiter a_waiter;
117 init_awaiter(&a_waiter, 0);
118 /* Note printing takes a few ms, so your printds won't be perfect. */
119 printd("[kernel] sys_block(), sleeping at %llu\n", read_tsc());
120 set_awaiter_rel(&a_waiter, usec);
121 set_alarm(tchain, &a_waiter);
122 sleep_on_awaiter(&a_waiter);
123 printd("[kernel] sys_block(), waking up at %llu\n", read_tsc());
127 // Writes 'val' to 'num_writes' entries of the well-known array in the kernel
128 // address space. It's just #defined to be some random 4MB chunk (which ought
129 // to be boot_alloced or something). Meant to grab exclusive access to cache
130 // lines, to simulate doing something useful.
131 static int sys_cache_buster(struct proc *p, uint32_t num_writes,
132 uint32_t num_pages, uint32_t flags)
133 { TRUSTEDBLOCK /* zra: this is not really part of the kernel */
134 #define BUSTER_ADDR 0xd0000000L // around 512 MB deep
135 #define MAX_WRITES 1048576*8
137 #define INSERT_ADDR (UINFO + 2*PGSIZE) // should be free for these tests
138 uint32_t* buster = (uint32_t*)BUSTER_ADDR;
139 static spinlock_t buster_lock = SPINLOCK_INITIALIZER;
141 page_t* a_page[MAX_PAGES];
143 /* Strided Accesses or Not (adjust to step by cachelines) */
145 if (flags & BUSTER_STRIDED) {
150 /* Shared Accesses or Not (adjust to use per-core regions)
151 * Careful, since this gives 8MB to each core, starting around 512MB.
152 * Also, doesn't separate memory for core 0 if it's an async call.
154 if (!(flags & BUSTER_SHARED))
155 buster = (uint32_t*)(BUSTER_ADDR + core_id() * 0x00800000);
157 /* Start the timer, if we're asked to print this info*/
158 if (flags & BUSTER_PRINT_TICKS)
159 ticks = start_timing();
161 /* Allocate num_pages (up to MAX_PAGES), to simulate doing some more
162 * realistic work. Note we don't write to these pages, even if we pick
163 * unshared. Mostly due to the inconvenience of having to match up the
164 * number of pages with the number of writes. And it's unnecessary.
167 spin_lock(&buster_lock);
168 for (int i = 0; i < MIN(num_pages, MAX_PAGES); i++) {
169 upage_alloc(p, &a_page[i],1);
170 page_insert(p->env_pgdir, a_page[i], (void*)INSERT_ADDR + PGSIZE*i,
172 page_decref(a_page[i]);
174 spin_unlock(&buster_lock);
177 if (flags & BUSTER_LOCKED)
178 spin_lock(&buster_lock);
179 for (int i = 0; i < MIN(num_writes, MAX_WRITES); i=i+stride)
180 buster[i] = 0xdeadbeef;
181 if (flags & BUSTER_LOCKED)
182 spin_unlock(&buster_lock);
185 spin_lock(&buster_lock);
186 for (int i = 0; i < MIN(num_pages, MAX_PAGES); i++) {
187 page_remove(p->env_pgdir, (void*)(INSERT_ADDR + PGSIZE * i));
188 page_decref(a_page[i]);
190 spin_unlock(&buster_lock);
194 if (flags & BUSTER_PRINT_TICKS) {
195 ticks = stop_timing(ticks);
196 printk("%llu,", ticks);
201 static int sys_cache_invalidate(void)
209 /* sys_reboot(): called directly from dispatch table. */
211 /* Print a string to the system console. */
212 static ssize_t sys_cputs(struct proc *p, const char *DANGEROUS string,
216 t_string = user_strdup_errno(p, string, strlen);
219 printk("%.*s", strlen, t_string);
220 user_memdup_free(p, t_string);
221 return (ssize_t)strlen;
224 // Read a character from the system console.
225 // Returns the character.
226 /* TODO: remove me */
227 static uint16_t sys_cgetc(struct proc *p)
231 // The cons_get_any_char() primitive doesn't wait for a character,
232 // but the sys_cgetc() system call does.
233 while ((c = cons_get_any_char()) == 0)
239 /* Returns the id of the physical core this syscall is executed on. */
240 static uint32_t sys_getpcoreid(void)
245 // TODO: Temporary hack until thread-local storage is implemented on i386 and
246 // this is removed from the user interface
247 static size_t sys_getvcoreid(struct proc *p)
249 return proc_get_vcoreid(p);
252 /************** Process management syscalls **************/
254 /* Returns the calling process's pid */
255 static pid_t sys_getpid(struct proc *p)
260 /* Creates a process from the file 'path'. The process is not runnable by
261 * default, so it needs it's status to be changed so that the next call to
262 * schedule() will try to run it. TODO: take args/envs from userspace. */
263 static int sys_proc_create(struct proc *p, char *path, size_t path_l,
268 struct file *program;
271 /* Copy in the path. Consider putting an upper bound on path_l. */
272 t_path = user_strdup_errno(p, path, path_l);
275 program = do_file_open(t_path, 0, 0);
276 user_memdup_free(p, t_path);
278 return -1; /* presumably, errno is already set */
279 /* TODO: need to split the proc creation, since you must load after setting
280 * args/env, since auxp gets set up there. */
281 //new_p = proc_create(program, 0, 0);
282 if (proc_alloc(&new_p, current))
284 /* Set the argument stuff needed by glibc */
285 if (memcpy_from_user_errno(p, new_p->procinfo->argp, pi->argp,
288 if (memcpy_from_user_errno(p, new_p->procinfo->argbuf, pi->argbuf,
291 if (load_elf(new_p, program))
293 kref_put(&program->f_kref);
294 /* Connect to stdin, stdout, stderr (part of proc_create()) */
295 assert(insert_file(&new_p->open_files, dev_stdin, 0) == 0);
296 assert(insert_file(&new_p->open_files, dev_stdout, 0) == 1);
297 assert(insert_file(&new_p->open_files, dev_stderr, 0) == 2);
300 proc_decref(new_p); /* give up the reference created in proc_create() */
304 proc_decref(new_p); /* give up the reference created in proc_create() */
306 kref_put(&program->f_kref);
310 /* Makes process PID runnable. Consider moving the functionality to process.c */
311 static error_t sys_proc_run(struct proc *p, unsigned pid)
313 struct proc *target = pid2proc(pid);
320 /* make sure we have access and it's in the right state to be activated */
321 if (!proc_controls(p, target)) {
324 } else if (target->state != PROC_CREATED) {
328 /* Note a proc can spam this for someone it controls. Seems safe - if it
329 * isn't we can change it. */
338 /* Destroy proc pid. If this is called by the dying process, it will never
339 * return. o/w it will return 0 on success, or an error. Errors include:
340 * - ESRCH: if there is no such process with pid
341 * - EPERM: if caller does not control pid */
342 static error_t sys_proc_destroy(struct proc *p, pid_t pid, int exitcode)
345 struct proc *p_to_die = pid2proc(pid);
351 if (!proc_controls(p, p_to_die)) {
352 proc_decref(p_to_die);
357 p->exitcode = exitcode;
358 printd("[PID %d] proc exiting gracefully (code %d)\n", p->pid,exitcode);
360 p_to_die->exitcode = exitcode; /* so its parent has some clue */
361 printd("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
363 proc_destroy(p_to_die);
364 /* we only get here if we weren't the one to die */
365 proc_decref(p_to_die);
369 static int sys_proc_yield(struct proc *p, bool being_nice)
371 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
372 /* proc_yield() often doesn't return - we need to set the syscall retval
373 * early. If it doesn't return, it expects to eat our reference (for now).
375 finish_sysc(pcpui->cur_sysc, pcpui->cur_proc);
376 pcpui->cur_sysc = 0; /* don't touch sysc again */
378 proc_yield(p, being_nice);
380 /* Shouldn't return, to prevent the chance of mucking with cur_sysc. */
385 static int sys_change_vcore(struct proc *p, uint32_t vcoreid,
386 bool enable_my_notif)
388 /* Note retvals can be negative, but we don't mess with errno in case
389 * callers use this in low-level code and want to extract the 'errno'. */
390 return proc_change_to_vcore(p, vcoreid, enable_my_notif);
393 static ssize_t sys_fork(env_t* e)
397 // TODO: right now we only support fork for single-core processes
398 if (e->state != PROC_RUNNING_S) {
403 assert(!proc_alloc(&env, current));
406 env->heap_top = e->heap_top;
408 disable_irqsave(&state); /* protect cur_tf */
409 /* Can't really fork if we don't have a current_tf to fork */
414 env->env_tf = *current_tf;
415 enable_irqsave(&state);
417 env->cache_colors_map = cache_colors_map_alloc();
418 for(int i=0; i < llc_cache->num_colors; i++)
419 if(GET_BITMASK_BIT(e->cache_colors_map,i))
420 cache_color_alloc(llc_cache, env->cache_colors_map);
422 /* Make the new process have the same VMRs as the older. This will copy the
423 * contents of non MAP_SHARED pages to the new VMRs. */
424 if (duplicate_vmrs(e, env)) {
425 proc_destroy(env); /* this is prob what you want, not decref by 2 */
430 /* Switch to the new proc's address space and finish the syscall. We'll
431 * never naturally finish this syscall for the new proc, since its memory
432 * is cloned before we return for the original process. If we ever do CoW
433 * for forked memory, this will be the first place that gets CoW'd. */
434 temp = switch_to(env);
435 finish_current_sysc(0);
436 switch_back(env, temp);
438 /* In general, a forked process should be a fresh process, and we copy over
439 * whatever stuff is needed between procinfo/procdata. */
440 /* Copy over the procinfo argument stuff in case they don't exec */
441 memcpy(env->procinfo->argp, e->procinfo->argp, sizeof(e->procinfo->argp));
442 memcpy(env->procinfo->argbuf, e->procinfo->argbuf,
443 sizeof(e->procinfo->argbuf));
445 /* new guy needs to know about ldt (everything else in procdata is fresh */
446 env->procdata->ldt = e->procdata->ldt;
449 clone_files(&e->open_files, &env->open_files);
450 /* FYI: once we call ready, the proc is open for concurrent usage */
454 // don't decref the new process.
455 // that will happen when the parent waits for it.
456 // TODO: if the parent doesn't wait, we need to change the child's parent
457 // when the parent dies, or at least decref it
459 printd("[PID %d] fork PID %d\n",e->pid,env->pid);
463 /* Load the binary "path" into the current process, and start executing it.
464 * argv and envp are magically bundled in procinfo for now. Keep in sync with
465 * glibc's sysdeps/ros/execve.c. Once past a certain point, this function won't
466 * return. It assumes (and checks) that it is current. Don't give it an extra
467 * refcnt'd *p (syscall won't do that).
468 * Note: if someone batched syscalls with this call, they could clobber their
469 * old memory (and will likely PF and die). Don't do it... */
470 static int sys_exec(struct proc *p, char *path, size_t path_l,
475 struct file *program;
476 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
479 /* We probably want it to never be allowed to exec if it ever was _M */
480 if (p->state != PROC_RUNNING_S) {
484 if (p != pcpui->cur_proc) {
488 /* Copy in the path. Consider putting an upper bound on path_l. */
489 t_path = user_strdup_errno(p, path, path_l);
492 disable_irqsave(&state); /* protect cur_tf */
493 /* Can't exec if we don't have a current_tf to restart (if we fail). This
494 * isn't 100% true, but I'm okay with it. */
495 if (!pcpui->cur_tf) {
496 enable_irqsave(&state);
500 /* Preemptively copy out the cur_tf, in case we fail later (easier on cur_tf
501 * if we do this now) */
502 p->env_tf = *pcpui->cur_tf;
503 /* Clear the current_tf. We won't be returning the 'normal' way. Even if
504 * we want to return with an error, we need to go back differently in case
505 * we succeed. This needs to be done before we could possibly block, but
506 * unfortunately happens before the point of no return. */
508 enable_irqsave(&state);
509 /* This could block: */
510 program = do_file_open(t_path, 0, 0);
511 user_memdup_free(p, t_path);
514 /* Set the argument stuff needed by glibc */
515 if (memcpy_from_user_errno(p, p->procinfo->argp, pi->argp,
518 if (memcpy_from_user_errno(p, p->procinfo->argbuf, pi->argbuf,
521 /* This is the point of no return for the process. */
523 /* clear this, so the new program knows to get an LDT */
524 p->procdata->ldt = 0;
527 close_all_files(&p->open_files, TRUE);
528 env_user_mem_free(p, 0, UMAPTOP);
529 if (load_elf(p, program)) {
530 kref_put(&program->f_kref);
531 /* Note this is an inedible reference, but proc_destroy now returns */
533 /* We don't want to do anything else - we just need to not accidentally
534 * return to the user (hence the all_out) */
537 printd("[PID %d] exec %s\n", p->pid, file_name(program));
538 kref_put(&program->f_kref);
540 /* These error and out paths are so we can handle the async interface, both
541 * for when we want to error/return to the proc, as well as when we succeed
542 * and want to start the newly exec'd _S */
544 /* These two error paths are for when we want to restart the process with an
545 * error value (errno is already set). */
546 kref_put(&program->f_kref);
548 finish_current_sysc(-1);
550 /* Here's how we restart the new (on success) or old (on failure) proc: */
551 spin_lock(&p->proc_lock);
552 __unmap_vcore(p, 0); /* VC# keep in sync with proc_run_s */
553 __proc_set_state(p, PROC_WAITING); /* fake a yield */
554 spin_unlock(&p->proc_lock);
557 /* we can't return, since we'd write retvals to the old location of the
558 * syscall struct (which has been freed and is in the old userspace) (or has
559 * already been written to).*/
560 disable_irq(); /* abandon_core/clear_own wants irqs disabled */
561 clear_owning_proc(core_id());
563 smp_idle(); /* will reenable interrupts */
566 /* Note: we only allow waiting on children (no such thing as threads, for
567 * instance). Right now we only allow waiting on termination (not signals),
568 * and we don't have a way for parents to disown their children (such as
569 * ignoring SIGCHLD, see man 2 waitpid's Notes). */
570 static int sys_trywait(struct proc *parent, pid_t pid, int *status)
573 * - WAIT should handle stop and start via signal too
574 * - what semantics? need a wait for every change to state? etc.
575 * - should have an option for WNOHANG, and a bunch of other things.
576 * - think about what functions we want to work with MCPS
578 struct proc* child = pid2proc(pid);
583 set_errno(ECHILD); /* ECHILD also used for no proc */
586 if (!(parent->pid == child->ppid)) {
590 /* Block til there is some activity (DYING for now) */
591 if (!(child->state == PROC_DYING)) {
592 sleep_on(&child->state_change);
595 assert(child->state == PROC_DYING);
596 ret_status = child->exitcode;
597 /* wait succeeded - need to clean up the proc. */
598 proc_disown_child(parent, child);
601 /* ignoring the retval here - don't care if they have a bad addr. */
602 memcpy_to_user(parent, status, &ret_status, sizeof(ret_status));
603 printd("[PID %d] waited for PID %d (code %d)\n", parent->pid,
612 /************** Memory Management Syscalls **************/
614 static void *sys_mmap(struct proc *p, uintptr_t addr, size_t len, int prot,
615 int flags, int fd, off_t offset)
617 return mmap(p, addr, len, prot, flags, fd, offset);
620 static intreg_t sys_mprotect(struct proc *p, void *addr, size_t len, int prot)
622 return mprotect(p, (uintptr_t)addr, len, prot);
625 static intreg_t sys_munmap(struct proc *p, void *addr, size_t len)
627 return munmap(p, (uintptr_t)addr, len);
630 static ssize_t sys_shared_page_alloc(env_t* p1,
631 void**DANGEROUS _addr, pid_t p2_id,
632 int p1_flags, int p2_flags
635 printk("[kernel] shared page alloc is deprecated/unimplemented.\n");
639 static int sys_shared_page_free(env_t* p1, void*DANGEROUS addr, pid_t p2)
644 /* Untested. Will notify the target on the given vcore, if the caller controls
645 * the target. Will honor the target's wanted/vcoreid. u_ne can be NULL. */
646 static int sys_notify(struct proc *p, int target_pid, unsigned int ev_type,
647 struct event_msg *u_msg)
649 struct event_msg local_msg = {0};
650 struct proc *target = pid2proc(target_pid);
655 if (!proc_controls(p, target)) {
660 /* if the user provided an ev_msg, copy it in and use that */
662 if (memcpy_from_user(p, &local_msg, u_msg, sizeof(struct event_msg))) {
668 local_msg.ev_type = ev_type;
670 send_kernel_event(target, &local_msg, 0);
675 /* Will notify the calling process on the given vcore, independently of WANTED
676 * or advertised vcoreid. If you change the parameters, change pop_ros_tf() */
677 static int sys_self_notify(struct proc *p, uint32_t vcoreid,
678 unsigned int ev_type, struct event_msg *u_msg,
681 struct event_msg local_msg = {0};
683 printd("[kernel] received self notify for vcoreid %d, type %d, msg %08p\n",
684 vcoreid, ev_type, u_msg);
685 /* if the user provided an ev_msg, copy it in and use that */
687 if (memcpy_from_user(p, &local_msg, u_msg, sizeof(struct event_msg))) {
692 local_msg.ev_type = ev_type;
694 /* this will post a message and IPI, regardless of wants/needs/debutantes.*/
695 post_vcore_event(p, &local_msg, vcoreid, priv ? EVENT_VCORE_PRIVATE : 0);
696 proc_notify(p, vcoreid);
700 /* This will set a local timer for usec, then shut down the core. There's a
701 * slight race between spinner and halt. For now, the core will wake up for
702 * other interrupts and service them, but will not process routine messages or
703 * do anything other than halt until the alarm goes off. We could just unset
704 * the alarm and return early. On hardware, there are a lot of interrupts that
705 * come in. If we ever use this, we can take a closer look. */
706 static int sys_halt_core(struct proc *p, unsigned int usec)
708 struct timer_chain *tchain = &per_cpu_info[core_id()].tchain;
709 struct alarm_waiter a_waiter;
711 void unblock(struct alarm_waiter *waiter)
715 init_awaiter(&a_waiter, unblock);
716 set_awaiter_rel(&a_waiter, MAX(usec, 100));
717 set_alarm(tchain, &a_waiter);
719 /* Could wake up due to another interrupt, but we want to sleep still. */
721 cpu_halt(); /* slight race between spinner and halt */
724 printd("Returning from halting\n");
728 /* Changes a process into _M mode, or -EINVAL if it already is an mcp.
729 * __proc_change_to_m() returns and we'll eventually finish the sysc later. The
730 * original context may restart on a remote core before we return and finish,
731 * but that's fine thanks to the async kernel interface. */
732 static int sys_change_to_m(struct proc *p)
734 int retval = proc_change_to_m(p);
735 /* convert the kernel error code into (-1, errno) */
743 /* Not sure what people will need. For now, they can send in the resource they
744 * want. Up to the ksched to support this, and other things (like -1 for all
745 * resources). Might have this info go in via procdata instead. */
746 static int sys_poke_ksched(struct proc *p, int res_type)
748 poke_ksched(p, res_type);
752 /************** Platform Specific Syscalls **************/
754 //Read a buffer over the serial port
755 static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
757 printk("[kernel] serial reading is deprecated.\n");
761 #ifdef __CONFIG_SERIAL_IO__
762 char *COUNT(len) buf = user_mem_assert(e, _buf, len, 1, PTE_USER_RO);
763 size_t bytes_read = 0;
765 while((c = serial_read_byte()) != -1) {
766 buf[bytes_read++] = (uint8_t)c;
767 if(bytes_read == len) break;
769 return (ssize_t)bytes_read;
775 //Write a buffer over the serial port
776 static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
778 printk("[kernel] serial writing is deprecated.\n");
781 #ifdef __CONFIG_SERIAL_IO__
782 char *COUNT(len) _buf = user_mem_assert(e, buf, len, 1, PTE_USER_RO);
783 for(int i =0; i<len; i++)
784 serial_send_byte(buf[i]);
791 #ifdef __CONFIG_NETWORKING__
792 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
793 static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
800 spin_lock(&packet_buffers_lock);
802 if (num_packet_buffers == 0) {
803 spin_unlock(&packet_buffers_lock);
807 ptr = packet_buffers[packet_buffers_head];
808 len = packet_buffers_sizes[packet_buffers_head];
810 num_packet_buffers--;
811 packet_buffers_head = (packet_buffers_head + 1) % MAX_PACKET_BUFFERS;
813 spin_unlock(&packet_buffers_lock);
815 char* _buf = user_mem_assert(e, buf, len, 1, PTE_U);
817 memcpy(_buf, ptr, len);
827 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
828 static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
835 // HACK TO BYPASS HACK
836 int just_sent = send_frame(buf, len);
839 printk("Packet send fail\n");
845 // END OF RECURSIVE HACK
847 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_U);
850 int cur_packet_len = 0;
851 while (total_sent != len) {
852 cur_packet_len = ((len - total_sent) > MTU) ? MTU : (len - total_sent);
853 char dest_mac[6] = APPSERVER_MAC_ADDRESS;
854 char* wrap_buffer = eth_wrap(_buf + total_sent, cur_packet_len, device_mac, dest_mac, APPSERVER_PORT);
855 just_sent = send_frame(wrap_buffer, cur_packet_len + sizeof(struct ETH_Header));
858 return 0; // This should be an error code of its own
863 total_sent += cur_packet_len;
873 static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf)
876 for (int i = 0; i < 6; i++)
877 buf[i] = device_mac[i];
884 static int sys_eth_recv_check(env_t* e)
886 if (num_packet_buffers != 0)
894 static intreg_t sys_read(struct proc *p, int fd, void *buf, int len)
897 struct file *file = get_file_from_fd(&p->open_files, fd);
902 if (!file->f_op->read) {
903 kref_put(&file->f_kref);
907 /* TODO: (UMEM) currently, read() handles user memcpy issues, but we
908 * probably should user_mem_check and pin the region here, so read doesn't
910 ret = file->f_op->read(file, buf, len, &file->f_pos);
911 kref_put(&file->f_kref);
915 static intreg_t sys_write(struct proc *p, int fd, const void *buf, int len)
918 struct file *file = get_file_from_fd(&p->open_files, fd);
923 if (!file->f_op->write) {
924 kref_put(&file->f_kref);
929 ret = file->f_op->write(file, buf, len, &file->f_pos);
930 kref_put(&file->f_kref);
934 /* Checks args/reads in the path, opens the file, and inserts it into the
935 * process's open file list.
937 * TODO: take the path length */
938 static intreg_t sys_open(struct proc *p, const char *path, size_t path_l,
944 printd("File %s Open attempt\n", path);
945 char *t_path = user_strdup_errno(p, path, path_l);
948 mode &= ~p->fs_env.umask;
949 file = do_file_open(t_path, oflag, mode);
950 user_memdup_free(p, t_path);
953 fd = insert_file(&p->open_files, file, 0); /* stores the ref to file */
954 kref_put(&file->f_kref);
956 warn("File insertion failed");
959 printd("File %s Open, res=%d\n", path, fd);
963 static intreg_t sys_close(struct proc *p, int fd)
965 struct file *file = put_file_from_fd(&p->open_files, fd);
973 /* kept around til we remove the last ufe */
974 #define ufe(which,a0,a1,a2,a3) \
975 frontend_syscall_errno(p,APPSERVER_SYSCALL_##which,\
976 (int)(a0),(int)(a1),(int)(a2),(int)(a3))
978 static intreg_t sys_fstat(struct proc *p, int fd, struct kstat *u_stat)
981 struct file *file = get_file_from_fd(&p->open_files, fd);
986 kbuf = kmalloc(sizeof(struct kstat), 0);
988 kref_put(&file->f_kref);
992 stat_inode(file->f_dentry->d_inode, kbuf);
993 kref_put(&file->f_kref);
994 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
995 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
1004 /* sys_stat() and sys_lstat() do nearly the same thing, differing in how they
1005 * treat a symlink for the final item, which (probably) will be controlled by
1006 * the lookup flags */
1007 static intreg_t stat_helper(struct proc *p, const char *path, size_t path_l,
1008 struct kstat *u_stat, int flags)
1011 struct dentry *path_d;
1012 char *t_path = user_strdup_errno(p, path, path_l);
1015 path_d = lookup_dentry(t_path, flags);
1016 user_memdup_free(p, t_path);
1019 kbuf = kmalloc(sizeof(struct kstat), 0);
1022 kref_put(&path_d->d_kref);
1025 stat_inode(path_d->d_inode, kbuf);
1026 kref_put(&path_d->d_kref);
1027 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
1028 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
1037 /* Follow a final symlink */
1038 static intreg_t sys_stat(struct proc *p, const char *path, size_t path_l,
1039 struct kstat *u_stat)
1041 return stat_helper(p, path, path_l, u_stat, LOOKUP_FOLLOW);
1044 /* Don't follow a final symlink */
1045 static intreg_t sys_lstat(struct proc *p, const char *path, size_t path_l,
1046 struct kstat *u_stat)
1048 return stat_helper(p, path, path_l, u_stat, 0);
1051 intreg_t sys_fcntl(struct proc *p, int fd, int cmd, int arg)
1054 struct file *file = get_file_from_fd(&p->open_files, fd);
1061 retval = insert_file(&p->open_files, file, arg);
1068 retval = p->open_files.fd[fd].fd_flags;
1071 if (arg == FD_CLOEXEC)
1072 file->f_flags |= O_CLOEXEC;
1075 retval = file->f_flags;
1078 /* only allowed to set certain flags. */
1079 arg &= O_FCNTL_FLAGS;
1080 file->f_flags = (file->f_flags & ~O_FCNTL_FLAGS) | arg;
1083 warn("Unsupported fcntl cmd %d\n", cmd);
1085 kref_put(&file->f_kref);
1089 static intreg_t sys_access(struct proc *p, const char *path, size_t path_l,
1093 char *t_path = user_strdup_errno(p, path, path_l);
1096 retval = do_access(t_path, mode);
1097 user_memdup_free(p, t_path);
1098 printd("Access for path: %s retval: %d\n", path, retval);
1106 intreg_t sys_umask(struct proc *p, int mask)
1108 int old_mask = p->fs_env.umask;
1109 p->fs_env.umask = mask & S_PMASK;
1113 intreg_t sys_chmod(struct proc *p, const char *path, size_t path_l, int mode)
1116 char *t_path = user_strdup_errno(p, path, path_l);
1119 retval = do_chmod(t_path, mode);
1120 user_memdup_free(p, t_path);
1128 static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
1131 struct file *file = get_file_from_fd(&p->open_files, fd);
1136 ret = file->f_op->llseek(file, offset, whence);
1137 kref_put(&file->f_kref);
1141 intreg_t sys_link(struct proc *p, char *old_path, size_t old_l,
1142 char *new_path, size_t new_l)
1145 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1146 if (t_oldpath == NULL)
1148 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1149 if (t_newpath == NULL) {
1150 user_memdup_free(p, t_oldpath);
1153 ret = do_link(t_oldpath, t_newpath);
1154 user_memdup_free(p, t_oldpath);
1155 user_memdup_free(p, t_newpath);
1159 intreg_t sys_unlink(struct proc *p, const char *path, size_t path_l)
1162 char *t_path = user_strdup_errno(p, path, path_l);
1165 retval = do_unlink(t_path);
1166 user_memdup_free(p, t_path);
1170 intreg_t sys_symlink(struct proc *p, char *old_path, size_t old_l,
1171 char *new_path, size_t new_l)
1174 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1175 if (t_oldpath == NULL)
1177 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1178 if (t_newpath == NULL) {
1179 user_memdup_free(p, t_oldpath);
1182 ret = do_symlink(new_path, old_path, S_IRWXU | S_IRWXG | S_IRWXO);
1183 user_memdup_free(p, t_oldpath);
1184 user_memdup_free(p, t_newpath);
1188 intreg_t sys_readlink(struct proc *p, char *path, size_t path_l,
1189 char *u_buf, size_t buf_l)
1193 struct dentry *path_d;
1194 char *t_path = user_strdup_errno(p, path, path_l);
1197 path_d = lookup_dentry(t_path, 0);
1198 user_memdup_free(p, t_path);
1201 symname = path_d->d_inode->i_op->readlink(path_d);
1202 copy_amt = strnlen(symname, buf_l - 1) + 1;
1203 if (memcpy_to_user_errno(p, u_buf, symname, copy_amt)) {
1204 kref_put(&path_d->d_kref);
1208 kref_put(&path_d->d_kref);
1209 printd("READLINK returning %s\n", u_buf);
1213 intreg_t sys_chdir(struct proc *p, const char *path, size_t path_l)
1216 char *t_path = user_strdup_errno(p, path, path_l);
1219 retval = do_chdir(&p->fs_env, t_path);
1220 user_memdup_free(p, t_path);
1228 /* Note cwd_l is not a strlen, it's an absolute size */
1229 intreg_t sys_getcwd(struct proc *p, char *u_cwd, size_t cwd_l)
1233 char *k_cwd = do_getcwd(&p->fs_env, &kfree_this, cwd_l);
1235 return -1; /* errno set by do_getcwd */
1236 if (memcpy_to_user_errno(p, u_cwd, k_cwd, strnlen(k_cwd, cwd_l - 1) + 1))
1242 intreg_t sys_mkdir(struct proc *p, const char *path, size_t path_l, int mode)
1245 char *t_path = user_strdup_errno(p, path, path_l);
1248 mode &= ~p->fs_env.umask;
1249 retval = do_mkdir(t_path, mode);
1250 user_memdup_free(p, t_path);
1254 intreg_t sys_rmdir(struct proc *p, const char *path, size_t path_l)
1257 char *t_path = user_strdup_errno(p, path, path_l);
1260 retval = do_rmdir(t_path);
1261 user_memdup_free(p, t_path);
1265 intreg_t sys_gettimeofday(struct proc *p, int *buf)
1267 static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
1270 spin_lock(>od_lock);
1273 #if (defined __CONFIG_APPSERVER__)
1274 t0 = ufe(time,0,0,0,0);
1276 // Nanwan's birthday, bitches!!
1279 spin_unlock(>od_lock);
1281 long long dt = read_tsc();
1282 /* TODO: This probably wants its own function, using a struct timeval */
1283 int kbuf[2] = {t0+dt/system_timing.tsc_freq,
1284 (dt%system_timing.tsc_freq)*1000000/system_timing.tsc_freq};
1286 return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
1289 intreg_t sys_tcgetattr(struct proc *p, int fd, void *termios_p)
1292 /* TODO: actually support this call on tty FDs. Right now, we just fake
1293 * what my linux box reports for a bash pty. */
1294 struct termios *kbuf = kmalloc(sizeof(struct termios), 0);
1295 kbuf->c_iflag = 0x2d02;
1296 kbuf->c_oflag = 0x0005;
1297 kbuf->c_cflag = 0x04bf;
1298 kbuf->c_lflag = 0x8a3b;
1300 kbuf->c_ispeed = 0xf;
1301 kbuf->c_ospeed = 0xf;
1302 kbuf->c_cc[0] = 0x03;
1303 kbuf->c_cc[1] = 0x1c;
1304 kbuf->c_cc[2] = 0x7f;
1305 kbuf->c_cc[3] = 0x15;
1306 kbuf->c_cc[4] = 0x04;
1307 kbuf->c_cc[5] = 0x00;
1308 kbuf->c_cc[6] = 0x01;
1309 kbuf->c_cc[7] = 0xff;
1310 kbuf->c_cc[8] = 0x11;
1311 kbuf->c_cc[9] = 0x13;
1312 kbuf->c_cc[10] = 0x1a;
1313 kbuf->c_cc[11] = 0xff;
1314 kbuf->c_cc[12] = 0x12;
1315 kbuf->c_cc[13] = 0x0f;
1316 kbuf->c_cc[14] = 0x17;
1317 kbuf->c_cc[15] = 0x16;
1318 kbuf->c_cc[16] = 0xff;
1319 kbuf->c_cc[17] = 0x00;
1320 kbuf->c_cc[18] = 0x00;
1321 kbuf->c_cc[19] = 0x00;
1322 kbuf->c_cc[20] = 0x00;
1323 kbuf->c_cc[21] = 0x00;
1324 kbuf->c_cc[22] = 0x00;
1325 kbuf->c_cc[23] = 0x00;
1326 kbuf->c_cc[24] = 0x00;
1327 kbuf->c_cc[25] = 0x00;
1328 kbuf->c_cc[26] = 0x00;
1329 kbuf->c_cc[27] = 0x00;
1330 kbuf->c_cc[28] = 0x00;
1331 kbuf->c_cc[29] = 0x00;
1332 kbuf->c_cc[30] = 0x00;
1333 kbuf->c_cc[31] = 0x00;
1335 if (memcpy_to_user_errno(p, termios_p, kbuf, sizeof(struct termios)))
1341 intreg_t sys_tcsetattr(struct proc *p, int fd, int optional_actions,
1342 const void *termios_p)
1344 /* TODO: do this properly too. For now, we just say 'it worked' */
1348 /* TODO: we don't have any notion of UIDs or GIDs yet, but don't let that stop a
1349 * process from thinking it can do these. The other alternative is to have
1350 * glibc return 0 right away, though someone might want to do something with
1351 * these calls. Someday. */
1352 intreg_t sys_setuid(struct proc *p, uid_t uid)
1357 intreg_t sys_setgid(struct proc *p, gid_t gid)
1362 /************** Syscall Invokation **************/
1364 const static struct sys_table_entry syscall_table[] = {
1365 [SYS_null] = {(syscall_t)sys_null, "null"},
1366 [SYS_block] = {(syscall_t)sys_block, "block"},
1367 [SYS_cache_buster] = {(syscall_t)sys_cache_buster, "buster"},
1368 [SYS_cache_invalidate] = {(syscall_t)sys_cache_invalidate, "wbinv"},
1369 [SYS_reboot] = {(syscall_t)reboot, "reboot!"},
1370 [SYS_cputs] = {(syscall_t)sys_cputs, "cputs"},
1371 [SYS_cgetc] = {(syscall_t)sys_cgetc, "cgetc"},
1372 [SYS_getpcoreid] = {(syscall_t)sys_getpcoreid, "getpcoreid"},
1373 [SYS_getvcoreid] = {(syscall_t)sys_getvcoreid, "getvcoreid"},
1374 [SYS_getpid] = {(syscall_t)sys_getpid, "getpid"},
1375 [SYS_proc_create] = {(syscall_t)sys_proc_create, "proc_create"},
1376 [SYS_proc_run] = {(syscall_t)sys_proc_run, "proc_run"},
1377 [SYS_proc_destroy] = {(syscall_t)sys_proc_destroy, "proc_destroy"},
1378 [SYS_yield] = {(syscall_t)sys_proc_yield, "proc_yield"},
1379 [SYS_change_vcore] = {(syscall_t)sys_change_vcore, "change_vcore"},
1380 [SYS_fork] = {(syscall_t)sys_fork, "fork"},
1381 [SYS_exec] = {(syscall_t)sys_exec, "exec"},
1382 [SYS_trywait] = {(syscall_t)sys_trywait, "trywait"},
1383 [SYS_mmap] = {(syscall_t)sys_mmap, "mmap"},
1384 [SYS_munmap] = {(syscall_t)sys_munmap, "munmap"},
1385 [SYS_mprotect] = {(syscall_t)sys_mprotect, "mprotect"},
1386 [SYS_shared_page_alloc] = {(syscall_t)sys_shared_page_alloc, "pa"},
1387 [SYS_shared_page_free] = {(syscall_t)sys_shared_page_free, "pf"},
1388 [SYS_notify] = {(syscall_t)sys_notify, "notify"},
1389 [SYS_self_notify] = {(syscall_t)sys_self_notify, "self_notify"},
1390 [SYS_halt_core] = {(syscall_t)sys_halt_core, "halt_core"},
1391 #ifdef __CONFIG_SERIAL_IO__
1392 [SYS_serial_read] = {(syscall_t)sys_serial_read, "ser_read"},
1393 [SYS_serial_write] = {(syscall_t)sys_serial_write, "ser_write"},
1395 #ifdef __CONFIG_NETWORKING__
1396 [SYS_eth_read] = {(syscall_t)sys_eth_read, "eth_read"},
1397 [SYS_eth_write] = {(syscall_t)sys_eth_write, "eth_write"},
1398 [SYS_eth_get_mac_addr] = {(syscall_t)sys_eth_get_mac_addr, "get_mac"},
1399 [SYS_eth_recv_check] = {(syscall_t)sys_eth_recv_check, "recv_check"},
1401 #ifdef __CONFIG_ARSC_SERVER__
1402 [SYS_init_arsc] = {(syscall_t)sys_init_arsc, "init_arsc"},
1404 [SYS_change_to_m] = {(syscall_t)sys_change_to_m, "change_to_m"},
1405 [SYS_poke_ksched] = {(syscall_t)sys_poke_ksched, "poke_ksched"},
1406 [SYS_read] = {(syscall_t)sys_read, "read"},
1407 [SYS_write] = {(syscall_t)sys_write, "write"},
1408 [SYS_open] = {(syscall_t)sys_open, "open"},
1409 [SYS_close] = {(syscall_t)sys_close, "close"},
1410 [SYS_fstat] = {(syscall_t)sys_fstat, "fstat"},
1411 [SYS_stat] = {(syscall_t)sys_stat, "stat"},
1412 [SYS_lstat] = {(syscall_t)sys_lstat, "lstat"},
1413 [SYS_fcntl] = {(syscall_t)sys_fcntl, "fcntl"},
1414 [SYS_access] = {(syscall_t)sys_access, "access"},
1415 [SYS_umask] = {(syscall_t)sys_umask, "umask"},
1416 [SYS_chmod] = {(syscall_t)sys_chmod, "chmod"},
1417 [SYS_lseek] = {(syscall_t)sys_lseek, "lseek"},
1418 [SYS_link] = {(syscall_t)sys_link, "link"},
1419 [SYS_unlink] = {(syscall_t)sys_unlink, "unlink"},
1420 [SYS_symlink] = {(syscall_t)sys_symlink, "symlink"},
1421 [SYS_readlink] = {(syscall_t)sys_readlink, "readlink"},
1422 [SYS_chdir] = {(syscall_t)sys_chdir, "chdir"},
1423 [SYS_getcwd] = {(syscall_t)sys_getcwd, "getcwd"},
1424 [SYS_mkdir] = {(syscall_t)sys_mkdir, "mkdri"},
1425 [SYS_rmdir] = {(syscall_t)sys_rmdir, "rmdir"},
1426 [SYS_gettimeofday] = {(syscall_t)sys_gettimeofday, "gettime"},
1427 [SYS_tcgetattr] = {(syscall_t)sys_tcgetattr, "tcgetattr"},
1428 [SYS_tcsetattr] = {(syscall_t)sys_tcsetattr, "tcsetattr"},
1429 [SYS_setuid] = {(syscall_t)sys_setuid, "setuid"},
1430 [SYS_setgid] = {(syscall_t)sys_setgid, "setgid"}
1433 /* Executes the given syscall.
1435 * Note tf is passed in, which points to the tf of the context on the kernel
1436 * stack. If any syscall needs to block, it needs to save this info, as well as
1439 * This syscall function is used by both local syscall and arsc, and should
1440 * remain oblivious of the caller. */
1441 intreg_t syscall(struct proc *p, uintreg_t sc_num, uintreg_t a0, uintreg_t a1,
1442 uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
1444 const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
1446 uint32_t coreid, vcoreid;
1447 if (systrace_flags & SYSTRACE_ON) {
1448 if ((systrace_flags & SYSTRACE_ALLPROC) || (proc_is_traced(p))) {
1450 vcoreid = proc_get_vcoreid(p);
1451 if (systrace_flags & SYSTRACE_LOUD) {
1452 printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, "
1453 "%08p, %08p) proc: %d core: %d vcore: %d\n", read_tsc(),
1454 sc_num, syscall_table[sc_num].name, a0, a1, a2, a3,
1455 a4, a5, p->pid, coreid, vcoreid);
1457 struct systrace_record *trace;
1458 uintptr_t idx, new_idx;
1460 idx = systrace_bufidx;
1461 new_idx = (idx + 1) % systrace_bufsize;
1462 } while (!atomic_cas_u32(&systrace_bufidx, idx, new_idx));
1463 trace = &systrace_buffer[idx];
1464 trace->timestamp = read_tsc();
1465 trace->syscallno = sc_num;
1472 trace->pid = p->pid;
1473 trace->coreid = coreid;
1474 trace->vcoreid = vcoreid;
1478 if (sc_num > max_syscall || syscall_table[sc_num].call == NULL)
1479 panic("Invalid syscall number %d for proc %x!", sc_num, p);
1481 return syscall_table[sc_num].call(p, a0, a1, a2, a3, a4, a5);
1484 /* Execute the syscall on the local core */
1485 void run_local_syscall(struct syscall *sysc)
1487 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
1489 /* TODO: (UMEM) assert / pin the memory for the sysc */
1490 assert(irq_is_enabled()); /* in case we proc destroy */
1491 user_mem_assert(pcpui->cur_proc, sysc, sizeof(struct syscall),
1492 sizeof(uintptr_t), PTE_USER_RW);
1493 pcpui->cur_sysc = sysc; /* let the core know which sysc it is */
1494 sysc->retval = syscall(pcpui->cur_proc, sysc->num, sysc->arg0, sysc->arg1,
1495 sysc->arg2, sysc->arg3, sysc->arg4, sysc->arg5);
1496 /* Need to re-load pcpui, in case we migrated */
1497 pcpui = &per_cpu_info[core_id()];
1498 finish_sysc(sysc, pcpui->cur_proc);
1499 /* Can unpin (UMEM) at this point */
1500 pcpui->cur_sysc = 0; /* no longer working on sysc */
1503 /* A process can trap and call this function, which will set up the core to
1504 * handle all the syscalls. a.k.a. "sys_debutante(needs, wants)". If there is
1505 * at least one, it will run it directly. */
1506 void prep_syscalls(struct proc *p, struct syscall *sysc, unsigned int nr_syscs)
1509 /* Careful with pcpui here, we could have migrated */
1512 /* For all after the first call, send ourselves a KMSG (TODO). */
1514 warn("Only one supported (Debutante calls: %d)\n", nr_syscs);
1515 /* Call the first one directly. (we already checked to make sure there is
1517 run_local_syscall(sysc);
1520 /* Call this when something happens on the syscall where userspace might want to
1521 * get signaled. Passing p, since the caller should know who the syscall
1522 * belongs to (probably is current).
1524 * You need to have SC_K_LOCK set when you call this. */
1525 void __signal_syscall(struct syscall *sysc, struct proc *p)
1527 struct event_queue *ev_q;
1528 struct event_msg local_msg;
1529 /* User sets the ev_q then atomically sets the flag (races with SC_DONE) */
1530 if (atomic_read(&sysc->flags) & SC_UEVENT) {
1531 rmb(); /* read the ev_q after reading the flag */
1534 memset(&local_msg, 0, sizeof(struct event_msg));
1535 local_msg.ev_type = EV_SYSCALL;
1536 local_msg.ev_arg3 = sysc;
1537 send_event(p, ev_q, &local_msg, 0);
1542 /* Syscall tracing */
1543 static void __init_systrace(void)
1545 systrace_buffer = kmalloc(MAX_SYSTRACES*sizeof(struct systrace_record), 0);
1546 if (!systrace_buffer)
1547 panic("Unable to alloc a trace buffer\n");
1548 systrace_bufidx = 0;
1549 systrace_bufsize = MAX_SYSTRACES;
1550 /* Note we never free the buffer - it's around forever. Feel free to change
1551 * this if you want to change the size or something dynamically. */
1554 /* If you call this while it is running, it will change the mode */
1555 void systrace_start(bool silent)
1557 static bool init = FALSE;
1558 spin_lock_irqsave(&systrace_lock);
1563 systrace_flags = silent ? SYSTRACE_ON : SYSTRACE_ON | SYSTRACE_LOUD;
1564 spin_unlock_irqsave(&systrace_lock);
1567 int systrace_reg(bool all, struct proc *p)
1570 spin_lock_irqsave(&systrace_lock);
1572 printk("Tracing syscalls for all processes\n");
1573 systrace_flags |= SYSTRACE_ALLPROC;
1576 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1577 if (!systrace_procs[i]) {
1578 printk("Tracing syscalls for process %d\n", p->pid);
1579 systrace_procs[i] = p;
1585 spin_unlock_irqsave(&systrace_lock);
1589 void systrace_stop(void)
1591 spin_lock_irqsave(&systrace_lock);
1593 for (int i = 0; i < MAX_NUM_TRACED; i++)
1594 systrace_procs[i] = 0;
1595 spin_unlock_irqsave(&systrace_lock);
1598 /* If you registered a process specifically, then you need to dereg it
1599 * specifically. Or just fully stop, which will do it for all. */
1600 int systrace_dereg(bool all, struct proc *p)
1602 spin_lock_irqsave(&systrace_lock);
1604 printk("No longer tracing syscalls for all processes.\n");
1605 systrace_flags &= ~SYSTRACE_ALLPROC;
1607 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1608 if (systrace_procs[i] == p) {
1609 systrace_procs[i] = 0;
1610 printk("No longer tracing syscalls for process %d\n", p->pid);
1614 spin_unlock_irqsave(&systrace_lock);
1618 /* Regardless of locking, someone could be writing into the buffer */
1619 void systrace_print(bool all, struct proc *p)
1621 spin_lock_irqsave(&systrace_lock);
1622 /* if you want to be clever, you could make this start from the earliest
1623 * timestamp and loop around. Careful of concurrent writes. */
1624 for (int i = 0; i < systrace_bufsize; i++)
1625 if (systrace_buffer[i].timestamp)
1626 printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, %08p,"
1627 "%08p) proc: %d core: %d vcore: %d\n",
1628 systrace_buffer[i].timestamp,
1629 systrace_buffer[i].syscallno,
1630 syscall_table[systrace_buffer[i].syscallno].name,
1631 systrace_buffer[i].arg0,
1632 systrace_buffer[i].arg1,
1633 systrace_buffer[i].arg2,
1634 systrace_buffer[i].arg3,
1635 systrace_buffer[i].arg4,
1636 systrace_buffer[i].arg5,
1637 systrace_buffer[i].pid,
1638 systrace_buffer[i].coreid,
1639 systrace_buffer[i].vcoreid);
1640 spin_unlock_irqsave(&systrace_lock);
1643 void systrace_clear_buffer(void)
1645 spin_lock_irqsave(&systrace_lock);
1646 memset(systrace_buffer, 0, sizeof(struct systrace_record) * MAX_SYSTRACES);
1647 spin_unlock_irqsave(&systrace_lock);