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 static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
569 * - WAIT should handle stop and start via signal too
570 * - what semantics? need a wait for every change to state? etc.
571 * - should have an option for WNOHANG, and a bunch of other things.
572 * - think about what functions we want to work with MCPS
574 struct proc* p = pid2proc(pid);
576 // TODO: this syscall is racy, so we only support for single-core procs
577 if(e->state != PROC_RUNNING_S)
580 // TODO: need to use errno properly. sadly, ROS error codes conflict..
586 if(current->pid == p->ppid)
588 /* Block til there is some activity */
589 if (!(p->state == PROC_DYING)) {
590 sleep_on(&p->state_change);
592 if(p->state == PROC_DYING)
594 memcpy_to_user(e,status,&p->exitcode,sizeof(int));
595 printd("[PID %d] waited for PID %d (code %d)\n",
596 e->pid,p->pid,p->exitcode);
601 warn("Should not have reached here.");
606 else // not a child of the calling process
612 // if the wait succeeded, decref twice
623 /************** Memory Management Syscalls **************/
625 static void *sys_mmap(struct proc *p, uintptr_t addr, size_t len, int prot,
626 int flags, int fd, off_t offset)
628 return mmap(p, addr, len, prot, flags, fd, offset);
631 static intreg_t sys_mprotect(struct proc *p, void *addr, size_t len, int prot)
633 return mprotect(p, (uintptr_t)addr, len, prot);
636 static intreg_t sys_munmap(struct proc *p, void *addr, size_t len)
638 return munmap(p, (uintptr_t)addr, len);
641 static ssize_t sys_shared_page_alloc(env_t* p1,
642 void**DANGEROUS _addr, pid_t p2_id,
643 int p1_flags, int p2_flags
646 printk("[kernel] shared page alloc is deprecated/unimplemented.\n");
650 static int sys_shared_page_free(env_t* p1, void*DANGEROUS addr, pid_t p2)
655 /* Untested. Will notify the target on the given vcore, if the caller controls
656 * the target. Will honor the target's wanted/vcoreid. u_ne can be NULL. */
657 static int sys_notify(struct proc *p, int target_pid, unsigned int ev_type,
658 struct event_msg *u_msg)
660 struct event_msg local_msg = {0};
661 struct proc *target = pid2proc(target_pid);
666 if (!proc_controls(p, target)) {
671 /* if the user provided an ev_msg, copy it in and use that */
673 if (memcpy_from_user(p, &local_msg, u_msg, sizeof(struct event_msg))) {
679 send_kernel_event(target, &local_msg, 0);
684 /* Will notify the calling process on the given vcore, independently of WANTED
685 * or advertised vcoreid. If you change the parameters, change pop_ros_tf() */
686 static int sys_self_notify(struct proc *p, uint32_t vcoreid,
687 unsigned int ev_type, struct event_msg *u_msg,
690 struct event_msg local_msg = {0};
692 printd("[kernel] received self notify for vcoreid %d, type %d, msg %08p\n",
693 vcoreid, ev_type, u_msg);
694 /* if the user provided an ev_msg, copy it in and use that */
696 if (memcpy_from_user(p, &local_msg, u_msg, sizeof(struct event_msg))) {
701 local_msg.ev_type = ev_type;
703 /* this will post a message and IPI, regardless of wants/needs/debutantes.*/
704 post_vcore_event(p, &local_msg, vcoreid, priv ? EVENT_VCORE_PRIVATE : 0);
705 proc_notify(p, vcoreid);
709 /* This will set a local timer for usec, then shut down the core. There's a
710 * slight race between spinner and halt. For now, the core will wake up for
711 * other interrupts and service them, but will not process routine messages or
712 * do anything other than halt until the alarm goes off. We could just unset
713 * the alarm and return early. On hardware, there are a lot of interrupts that
714 * come in. If we ever use this, we can take a closer look. */
715 static int sys_halt_core(struct proc *p, unsigned int usec)
717 struct timer_chain *tchain = &per_cpu_info[core_id()].tchain;
718 struct alarm_waiter a_waiter;
720 void unblock(struct alarm_waiter *waiter)
724 init_awaiter(&a_waiter, unblock);
725 set_awaiter_rel(&a_waiter, MAX(usec, 100));
726 set_alarm(tchain, &a_waiter);
728 /* Could wake up due to another interrupt, but we want to sleep still. */
730 cpu_halt(); /* slight race between spinner and halt */
733 printd("Returning from halting\n");
737 /* Changes a process into _M mode, or -EINVAL if it already is an mcp.
738 * __proc_change_to_m() returns and we'll eventually finish the sysc later. The
739 * original context may restart on a remote core before we return and finish,
740 * but that's fine thanks to the async kernel interface. */
741 static int sys_change_to_m(struct proc *p)
743 int retval = proc_change_to_m(p);
744 /* convert the kernel error code into (-1, errno) */
752 /* Not sure what people will need. For now, they can send in the resource they
753 * want. Up to the ksched to support this, and other things (like -1 for all
754 * resources). Might have this info go in via procdata instead. */
755 static int sys_poke_ksched(struct proc *p, int res_type)
757 poke_ksched(p, res_type);
761 /************** Platform Specific Syscalls **************/
763 //Read a buffer over the serial port
764 static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
766 printk("[kernel] serial reading is deprecated.\n");
770 #ifdef __CONFIG_SERIAL_IO__
771 char *COUNT(len) buf = user_mem_assert(e, _buf, len, 1, PTE_USER_RO);
772 size_t bytes_read = 0;
774 while((c = serial_read_byte()) != -1) {
775 buf[bytes_read++] = (uint8_t)c;
776 if(bytes_read == len) break;
778 return (ssize_t)bytes_read;
784 //Write a buffer over the serial port
785 static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
787 printk("[kernel] serial writing is deprecated.\n");
790 #ifdef __CONFIG_SERIAL_IO__
791 char *COUNT(len) _buf = user_mem_assert(e, buf, len, 1, PTE_USER_RO);
792 for(int i =0; i<len; i++)
793 serial_send_byte(buf[i]);
800 #ifdef __CONFIG_NETWORKING__
801 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
802 static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
809 spin_lock(&packet_buffers_lock);
811 if (num_packet_buffers == 0) {
812 spin_unlock(&packet_buffers_lock);
816 ptr = packet_buffers[packet_buffers_head];
817 len = packet_buffers_sizes[packet_buffers_head];
819 num_packet_buffers--;
820 packet_buffers_head = (packet_buffers_head + 1) % MAX_PACKET_BUFFERS;
822 spin_unlock(&packet_buffers_lock);
824 char* _buf = user_mem_assert(e, buf, len, 1, PTE_U);
826 memcpy(_buf, ptr, len);
836 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
837 static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
844 // HACK TO BYPASS HACK
845 int just_sent = send_frame(buf, len);
848 printk("Packet send fail\n");
854 // END OF RECURSIVE HACK
856 char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_U);
859 int cur_packet_len = 0;
860 while (total_sent != len) {
861 cur_packet_len = ((len - total_sent) > MTU) ? MTU : (len - total_sent);
862 char dest_mac[6] = APPSERVER_MAC_ADDRESS;
863 char* wrap_buffer = eth_wrap(_buf + total_sent, cur_packet_len, device_mac, dest_mac, APPSERVER_PORT);
864 just_sent = send_frame(wrap_buffer, cur_packet_len + sizeof(struct ETH_Header));
867 return 0; // This should be an error code of its own
872 total_sent += cur_packet_len;
882 static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf)
885 for (int i = 0; i < 6; i++)
886 buf[i] = device_mac[i];
893 static int sys_eth_recv_check(env_t* e)
895 if (num_packet_buffers != 0)
903 static intreg_t sys_read(struct proc *p, int fd, void *buf, int len)
906 struct file *file = get_file_from_fd(&p->open_files, fd);
911 if (!file->f_op->read) {
912 kref_put(&file->f_kref);
916 /* TODO: (UMEM) currently, read() handles user memcpy issues, but we
917 * probably should user_mem_check and pin the region here, so read doesn't
919 ret = file->f_op->read(file, buf, len, &file->f_pos);
920 kref_put(&file->f_kref);
924 static intreg_t sys_write(struct proc *p, int fd, const void *buf, int len)
927 struct file *file = get_file_from_fd(&p->open_files, fd);
932 if (!file->f_op->write) {
933 kref_put(&file->f_kref);
938 ret = file->f_op->write(file, buf, len, &file->f_pos);
939 kref_put(&file->f_kref);
943 /* Checks args/reads in the path, opens the file, and inserts it into the
944 * process's open file list.
946 * TODO: take the path length */
947 static intreg_t sys_open(struct proc *p, const char *path, size_t path_l,
953 printd("File %s Open attempt\n", path);
954 char *t_path = user_strdup_errno(p, path, path_l);
957 mode &= ~p->fs_env.umask;
958 file = do_file_open(t_path, oflag, mode);
959 user_memdup_free(p, t_path);
962 fd = insert_file(&p->open_files, file, 0); /* stores the ref to file */
963 kref_put(&file->f_kref);
965 warn("File insertion failed");
968 printd("File %s Open, res=%d\n", path, fd);
972 static intreg_t sys_close(struct proc *p, int fd)
974 struct file *file = put_file_from_fd(&p->open_files, fd);
982 /* kept around til we remove the last ufe */
983 #define ufe(which,a0,a1,a2,a3) \
984 frontend_syscall_errno(p,APPSERVER_SYSCALL_##which,\
985 (int)(a0),(int)(a1),(int)(a2),(int)(a3))
987 static intreg_t sys_fstat(struct proc *p, int fd, struct kstat *u_stat)
990 struct file *file = get_file_from_fd(&p->open_files, fd);
995 kbuf = kmalloc(sizeof(struct kstat), 0);
997 kref_put(&file->f_kref);
1001 stat_inode(file->f_dentry->d_inode, kbuf);
1002 kref_put(&file->f_kref);
1003 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
1004 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
1013 /* sys_stat() and sys_lstat() do nearly the same thing, differing in how they
1014 * treat a symlink for the final item, which (probably) will be controlled by
1015 * the lookup flags */
1016 static intreg_t stat_helper(struct proc *p, const char *path, size_t path_l,
1017 struct kstat *u_stat, int flags)
1020 struct dentry *path_d;
1021 char *t_path = user_strdup_errno(p, path, path_l);
1024 path_d = lookup_dentry(t_path, flags);
1025 user_memdup_free(p, t_path);
1028 kbuf = kmalloc(sizeof(struct kstat), 0);
1031 kref_put(&path_d->d_kref);
1034 stat_inode(path_d->d_inode, kbuf);
1035 kref_put(&path_d->d_kref);
1036 /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
1037 if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
1046 /* Follow a final symlink */
1047 static intreg_t sys_stat(struct proc *p, const char *path, size_t path_l,
1048 struct kstat *u_stat)
1050 return stat_helper(p, path, path_l, u_stat, LOOKUP_FOLLOW);
1053 /* Don't follow a final symlink */
1054 static intreg_t sys_lstat(struct proc *p, const char *path, size_t path_l,
1055 struct kstat *u_stat)
1057 return stat_helper(p, path, path_l, u_stat, 0);
1060 intreg_t sys_fcntl(struct proc *p, int fd, int cmd, int arg)
1063 struct file *file = get_file_from_fd(&p->open_files, fd);
1070 retval = insert_file(&p->open_files, file, arg);
1077 retval = p->open_files.fd[fd].fd_flags;
1080 if (arg == FD_CLOEXEC)
1081 file->f_flags |= O_CLOEXEC;
1084 retval = file->f_flags;
1087 /* only allowed to set certain flags. */
1088 arg &= O_FCNTL_FLAGS;
1089 file->f_flags = (file->f_flags & ~O_FCNTL_FLAGS) | arg;
1092 warn("Unsupported fcntl cmd %d\n", cmd);
1094 kref_put(&file->f_kref);
1098 static intreg_t sys_access(struct proc *p, const char *path, size_t path_l,
1102 char *t_path = user_strdup_errno(p, path, path_l);
1105 retval = do_access(t_path, mode);
1106 user_memdup_free(p, t_path);
1107 printd("Access for path: %s retval: %d\n", path, retval);
1115 intreg_t sys_umask(struct proc *p, int mask)
1117 int old_mask = p->fs_env.umask;
1118 p->fs_env.umask = mask & S_PMASK;
1122 intreg_t sys_chmod(struct proc *p, const char *path, size_t path_l, int mode)
1125 char *t_path = user_strdup_errno(p, path, path_l);
1128 retval = do_chmod(t_path, mode);
1129 user_memdup_free(p, t_path);
1137 static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
1140 struct file *file = get_file_from_fd(&p->open_files, fd);
1145 ret = file->f_op->llseek(file, offset, whence);
1146 kref_put(&file->f_kref);
1150 intreg_t sys_link(struct proc *p, char *old_path, size_t old_l,
1151 char *new_path, size_t new_l)
1154 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1155 if (t_oldpath == NULL)
1157 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1158 if (t_newpath == NULL) {
1159 user_memdup_free(p, t_oldpath);
1162 ret = do_link(t_oldpath, t_newpath);
1163 user_memdup_free(p, t_oldpath);
1164 user_memdup_free(p, t_newpath);
1168 intreg_t sys_unlink(struct proc *p, const char *path, size_t path_l)
1171 char *t_path = user_strdup_errno(p, path, path_l);
1174 retval = do_unlink(t_path);
1175 user_memdup_free(p, t_path);
1179 intreg_t sys_symlink(struct proc *p, char *old_path, size_t old_l,
1180 char *new_path, size_t new_l)
1183 char *t_oldpath = user_strdup_errno(p, old_path, old_l);
1184 if (t_oldpath == NULL)
1186 char *t_newpath = user_strdup_errno(p, new_path, new_l);
1187 if (t_newpath == NULL) {
1188 user_memdup_free(p, t_oldpath);
1191 ret = do_symlink(new_path, old_path, S_IRWXU | S_IRWXG | S_IRWXO);
1192 user_memdup_free(p, t_oldpath);
1193 user_memdup_free(p, t_newpath);
1197 intreg_t sys_readlink(struct proc *p, char *path, size_t path_l,
1198 char *u_buf, size_t buf_l)
1202 struct dentry *path_d;
1203 char *t_path = user_strdup_errno(p, path, path_l);
1206 path_d = lookup_dentry(t_path, 0);
1207 user_memdup_free(p, t_path);
1210 symname = path_d->d_inode->i_op->readlink(path_d);
1211 copy_amt = strnlen(symname, buf_l - 1) + 1;
1212 if (memcpy_to_user_errno(p, u_buf, symname, copy_amt)) {
1213 kref_put(&path_d->d_kref);
1217 kref_put(&path_d->d_kref);
1218 printd("READLINK returning %s\n", u_buf);
1222 intreg_t sys_chdir(struct proc *p, const char *path, size_t path_l)
1225 char *t_path = user_strdup_errno(p, path, path_l);
1228 retval = do_chdir(&p->fs_env, t_path);
1229 user_memdup_free(p, t_path);
1237 /* Note cwd_l is not a strlen, it's an absolute size */
1238 intreg_t sys_getcwd(struct proc *p, char *u_cwd, size_t cwd_l)
1242 char *k_cwd = do_getcwd(&p->fs_env, &kfree_this, cwd_l);
1244 return -1; /* errno set by do_getcwd */
1245 if (memcpy_to_user_errno(p, u_cwd, k_cwd, strnlen(k_cwd, cwd_l - 1) + 1))
1251 intreg_t sys_mkdir(struct proc *p, const char *path, size_t path_l, int mode)
1254 char *t_path = user_strdup_errno(p, path, path_l);
1257 mode &= ~p->fs_env.umask;
1258 retval = do_mkdir(t_path, mode);
1259 user_memdup_free(p, t_path);
1263 intreg_t sys_rmdir(struct proc *p, const char *path, size_t path_l)
1266 char *t_path = user_strdup_errno(p, path, path_l);
1269 retval = do_rmdir(t_path);
1270 user_memdup_free(p, t_path);
1274 intreg_t sys_gettimeofday(struct proc *p, int *buf)
1276 static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
1279 spin_lock(>od_lock);
1282 #if (defined __CONFIG_APPSERVER__)
1283 t0 = ufe(time,0,0,0,0);
1285 // Nanwan's birthday, bitches!!
1288 spin_unlock(>od_lock);
1290 long long dt = read_tsc();
1291 /* TODO: This probably wants its own function, using a struct timeval */
1292 int kbuf[2] = {t0+dt/system_timing.tsc_freq,
1293 (dt%system_timing.tsc_freq)*1000000/system_timing.tsc_freq};
1295 return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
1298 intreg_t sys_tcgetattr(struct proc *p, int fd, void *termios_p)
1301 /* TODO: actually support this call on tty FDs. Right now, we just fake
1302 * what my linux box reports for a bash pty. */
1303 struct termios *kbuf = kmalloc(sizeof(struct termios), 0);
1304 kbuf->c_iflag = 0x2d02;
1305 kbuf->c_oflag = 0x0005;
1306 kbuf->c_cflag = 0x04bf;
1307 kbuf->c_lflag = 0x8a3b;
1309 kbuf->c_ispeed = 0xf;
1310 kbuf->c_ospeed = 0xf;
1311 kbuf->c_cc[0] = 0x03;
1312 kbuf->c_cc[1] = 0x1c;
1313 kbuf->c_cc[2] = 0x7f;
1314 kbuf->c_cc[3] = 0x15;
1315 kbuf->c_cc[4] = 0x04;
1316 kbuf->c_cc[5] = 0x00;
1317 kbuf->c_cc[6] = 0x01;
1318 kbuf->c_cc[7] = 0xff;
1319 kbuf->c_cc[8] = 0x11;
1320 kbuf->c_cc[9] = 0x13;
1321 kbuf->c_cc[10] = 0x1a;
1322 kbuf->c_cc[11] = 0xff;
1323 kbuf->c_cc[12] = 0x12;
1324 kbuf->c_cc[13] = 0x0f;
1325 kbuf->c_cc[14] = 0x17;
1326 kbuf->c_cc[15] = 0x16;
1327 kbuf->c_cc[16] = 0xff;
1328 kbuf->c_cc[17] = 0x00;
1329 kbuf->c_cc[18] = 0x00;
1330 kbuf->c_cc[19] = 0x00;
1331 kbuf->c_cc[20] = 0x00;
1332 kbuf->c_cc[21] = 0x00;
1333 kbuf->c_cc[22] = 0x00;
1334 kbuf->c_cc[23] = 0x00;
1335 kbuf->c_cc[24] = 0x00;
1336 kbuf->c_cc[25] = 0x00;
1337 kbuf->c_cc[26] = 0x00;
1338 kbuf->c_cc[27] = 0x00;
1339 kbuf->c_cc[28] = 0x00;
1340 kbuf->c_cc[29] = 0x00;
1341 kbuf->c_cc[30] = 0x00;
1342 kbuf->c_cc[31] = 0x00;
1344 if (memcpy_to_user_errno(p, termios_p, kbuf, sizeof(struct termios)))
1350 intreg_t sys_tcsetattr(struct proc *p, int fd, int optional_actions,
1351 const void *termios_p)
1353 /* TODO: do this properly too. For now, we just say 'it worked' */
1357 /* TODO: we don't have any notion of UIDs or GIDs yet, but don't let that stop a
1358 * process from thinking it can do these. The other alternative is to have
1359 * glibc return 0 right away, though someone might want to do something with
1360 * these calls. Someday. */
1361 intreg_t sys_setuid(struct proc *p, uid_t uid)
1366 intreg_t sys_setgid(struct proc *p, gid_t gid)
1371 /************** Syscall Invokation **************/
1373 const static struct sys_table_entry syscall_table[] = {
1374 [SYS_null] = {(syscall_t)sys_null, "null"},
1375 [SYS_block] = {(syscall_t)sys_block, "block"},
1376 [SYS_cache_buster] = {(syscall_t)sys_cache_buster, "buster"},
1377 [SYS_cache_invalidate] = {(syscall_t)sys_cache_invalidate, "wbinv"},
1378 [SYS_reboot] = {(syscall_t)reboot, "reboot!"},
1379 [SYS_cputs] = {(syscall_t)sys_cputs, "cputs"},
1380 [SYS_cgetc] = {(syscall_t)sys_cgetc, "cgetc"},
1381 [SYS_getpcoreid] = {(syscall_t)sys_getpcoreid, "getpcoreid"},
1382 [SYS_getvcoreid] = {(syscall_t)sys_getvcoreid, "getvcoreid"},
1383 [SYS_getpid] = {(syscall_t)sys_getpid, "getpid"},
1384 [SYS_proc_create] = {(syscall_t)sys_proc_create, "proc_create"},
1385 [SYS_proc_run] = {(syscall_t)sys_proc_run, "proc_run"},
1386 [SYS_proc_destroy] = {(syscall_t)sys_proc_destroy, "proc_destroy"},
1387 [SYS_yield] = {(syscall_t)sys_proc_yield, "proc_yield"},
1388 [SYS_change_vcore] = {(syscall_t)sys_change_vcore, "change_vcore"},
1389 [SYS_fork] = {(syscall_t)sys_fork, "fork"},
1390 [SYS_exec] = {(syscall_t)sys_exec, "exec"},
1391 [SYS_trywait] = {(syscall_t)sys_trywait, "trywait"},
1392 [SYS_mmap] = {(syscall_t)sys_mmap, "mmap"},
1393 [SYS_munmap] = {(syscall_t)sys_munmap, "munmap"},
1394 [SYS_mprotect] = {(syscall_t)sys_mprotect, "mprotect"},
1395 [SYS_shared_page_alloc] = {(syscall_t)sys_shared_page_alloc, "pa"},
1396 [SYS_shared_page_free] = {(syscall_t)sys_shared_page_free, "pf"},
1397 [SYS_notify] = {(syscall_t)sys_notify, "notify"},
1398 [SYS_self_notify] = {(syscall_t)sys_self_notify, "self_notify"},
1399 [SYS_halt_core] = {(syscall_t)sys_halt_core, "halt_core"},
1400 #ifdef __CONFIG_SERIAL_IO__
1401 [SYS_serial_read] = {(syscall_t)sys_serial_read, "ser_read"},
1402 [SYS_serial_write] = {(syscall_t)sys_serial_write, "ser_write"},
1404 #ifdef __CONFIG_NETWORKING__
1405 [SYS_eth_read] = {(syscall_t)sys_eth_read, "eth_read"},
1406 [SYS_eth_write] = {(syscall_t)sys_eth_write, "eth_write"},
1407 [SYS_eth_get_mac_addr] = {(syscall_t)sys_eth_get_mac_addr, "get_mac"},
1408 [SYS_eth_recv_check] = {(syscall_t)sys_eth_recv_check, "recv_check"},
1410 #ifdef __CONFIG_ARSC_SERVER__
1411 [SYS_init_arsc] = {(syscall_t)sys_init_arsc, "init_arsc"},
1413 [SYS_change_to_m] = {(syscall_t)sys_change_to_m, "change_to_m"},
1414 [SYS_poke_ksched] = {(syscall_t)sys_poke_ksched, "poke_ksched"},
1415 [SYS_read] = {(syscall_t)sys_read, "read"},
1416 [SYS_write] = {(syscall_t)sys_write, "write"},
1417 [SYS_open] = {(syscall_t)sys_open, "open"},
1418 [SYS_close] = {(syscall_t)sys_close, "close"},
1419 [SYS_fstat] = {(syscall_t)sys_fstat, "fstat"},
1420 [SYS_stat] = {(syscall_t)sys_stat, "stat"},
1421 [SYS_lstat] = {(syscall_t)sys_lstat, "lstat"},
1422 [SYS_fcntl] = {(syscall_t)sys_fcntl, "fcntl"},
1423 [SYS_access] = {(syscall_t)sys_access, "access"},
1424 [SYS_umask] = {(syscall_t)sys_umask, "umask"},
1425 [SYS_chmod] = {(syscall_t)sys_chmod, "chmod"},
1426 [SYS_lseek] = {(syscall_t)sys_lseek, "lseek"},
1427 [SYS_link] = {(syscall_t)sys_link, "link"},
1428 [SYS_unlink] = {(syscall_t)sys_unlink, "unlink"},
1429 [SYS_symlink] = {(syscall_t)sys_symlink, "symlink"},
1430 [SYS_readlink] = {(syscall_t)sys_readlink, "readlink"},
1431 [SYS_chdir] = {(syscall_t)sys_chdir, "chdir"},
1432 [SYS_getcwd] = {(syscall_t)sys_getcwd, "getcwd"},
1433 [SYS_mkdir] = {(syscall_t)sys_mkdir, "mkdri"},
1434 [SYS_rmdir] = {(syscall_t)sys_rmdir, "rmdir"},
1435 [SYS_gettimeofday] = {(syscall_t)sys_gettimeofday, "gettime"},
1436 [SYS_tcgetattr] = {(syscall_t)sys_tcgetattr, "tcgetattr"},
1437 [SYS_tcsetattr] = {(syscall_t)sys_tcsetattr, "tcsetattr"},
1438 [SYS_setuid] = {(syscall_t)sys_setuid, "setuid"},
1439 [SYS_setgid] = {(syscall_t)sys_setgid, "setgid"}
1442 /* Executes the given syscall.
1444 * Note tf is passed in, which points to the tf of the context on the kernel
1445 * stack. If any syscall needs to block, it needs to save this info, as well as
1448 * This syscall function is used by both local syscall and arsc, and should
1449 * remain oblivious of the caller. */
1450 intreg_t syscall(struct proc *p, uintreg_t sc_num, uintreg_t a0, uintreg_t a1,
1451 uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
1453 const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
1455 uint32_t coreid, vcoreid;
1456 if (systrace_flags & SYSTRACE_ON) {
1457 if ((systrace_flags & SYSTRACE_ALLPROC) || (proc_is_traced(p))) {
1459 vcoreid = proc_get_vcoreid(p);
1460 if (systrace_flags & SYSTRACE_LOUD) {
1461 printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, "
1462 "%08p, %08p) proc: %d core: %d vcore: %d\n", read_tsc(),
1463 sc_num, syscall_table[sc_num].name, a0, a1, a2, a3,
1464 a4, a5, p->pid, coreid, vcoreid);
1466 struct systrace_record *trace;
1467 uintptr_t idx, new_idx;
1469 idx = systrace_bufidx;
1470 new_idx = (idx + 1) % systrace_bufsize;
1471 } while (!atomic_cas_u32(&systrace_bufidx, idx, new_idx));
1472 trace = &systrace_buffer[idx];
1473 trace->timestamp = read_tsc();
1474 trace->syscallno = sc_num;
1481 trace->pid = p->pid;
1482 trace->coreid = coreid;
1483 trace->vcoreid = vcoreid;
1487 if (sc_num > max_syscall || syscall_table[sc_num].call == NULL)
1488 panic("Invalid syscall number %d for proc %x!", sc_num, p);
1490 return syscall_table[sc_num].call(p, a0, a1, a2, a3, a4, a5);
1493 /* Execute the syscall on the local core */
1494 void run_local_syscall(struct syscall *sysc)
1496 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
1498 /* TODO: (UMEM) assert / pin the memory for the sysc */
1499 assert(irq_is_enabled()); /* in case we proc destroy */
1500 user_mem_assert(pcpui->cur_proc, sysc, sizeof(struct syscall),
1501 sizeof(uintptr_t), PTE_USER_RW);
1502 pcpui->cur_sysc = sysc; /* let the core know which sysc it is */
1503 sysc->retval = syscall(pcpui->cur_proc, sysc->num, sysc->arg0, sysc->arg1,
1504 sysc->arg2, sysc->arg3, sysc->arg4, sysc->arg5);
1505 /* Need to re-load pcpui, in case we migrated */
1506 pcpui = &per_cpu_info[core_id()];
1507 finish_sysc(sysc, pcpui->cur_proc);
1508 /* Can unpin (UMEM) at this point */
1509 pcpui->cur_sysc = 0; /* no longer working on sysc */
1512 /* A process can trap and call this function, which will set up the core to
1513 * handle all the syscalls. a.k.a. "sys_debutante(needs, wants)". If there is
1514 * at least one, it will run it directly. */
1515 void prep_syscalls(struct proc *p, struct syscall *sysc, unsigned int nr_syscs)
1518 /* Careful with pcpui here, we could have migrated */
1521 /* For all after the first call, send ourselves a KMSG (TODO). */
1523 warn("Only one supported (Debutante calls: %d)\n", nr_syscs);
1524 /* Call the first one directly. (we already checked to make sure there is
1526 run_local_syscall(sysc);
1529 /* Call this when something happens on the syscall where userspace might want to
1530 * get signaled. Passing p, since the caller should know who the syscall
1531 * belongs to (probably is current).
1533 * You need to have SC_K_LOCK set when you call this. */
1534 void __signal_syscall(struct syscall *sysc, struct proc *p)
1536 struct event_queue *ev_q;
1537 struct event_msg local_msg;
1538 /* User sets the ev_q then atomically sets the flag (races with SC_DONE) */
1539 if (atomic_read(&sysc->flags) & SC_UEVENT) {
1540 rmb(); /* read the ev_q after reading the flag */
1543 memset(&local_msg, 0, sizeof(struct event_msg));
1544 local_msg.ev_type = EV_SYSCALL;
1545 local_msg.ev_arg3 = sysc;
1546 send_event(p, ev_q, &local_msg, 0);
1551 /* Syscall tracing */
1552 static void __init_systrace(void)
1554 systrace_buffer = kmalloc(MAX_SYSTRACES*sizeof(struct systrace_record), 0);
1555 if (!systrace_buffer)
1556 panic("Unable to alloc a trace buffer\n");
1557 systrace_bufidx = 0;
1558 systrace_bufsize = MAX_SYSTRACES;
1559 /* Note we never free the buffer - it's around forever. Feel free to change
1560 * this if you want to change the size or something dynamically. */
1563 /* If you call this while it is running, it will change the mode */
1564 void systrace_start(bool silent)
1566 static bool init = FALSE;
1567 spin_lock_irqsave(&systrace_lock);
1572 systrace_flags = silent ? SYSTRACE_ON : SYSTRACE_ON | SYSTRACE_LOUD;
1573 spin_unlock_irqsave(&systrace_lock);
1576 int systrace_reg(bool all, struct proc *p)
1579 spin_lock_irqsave(&systrace_lock);
1581 printk("Tracing syscalls for all processes\n");
1582 systrace_flags |= SYSTRACE_ALLPROC;
1585 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1586 if (!systrace_procs[i]) {
1587 printk("Tracing syscalls for process %d\n", p->pid);
1588 systrace_procs[i] = p;
1594 spin_unlock_irqsave(&systrace_lock);
1598 void systrace_stop(void)
1600 spin_lock_irqsave(&systrace_lock);
1602 for (int i = 0; i < MAX_NUM_TRACED; i++)
1603 systrace_procs[i] = 0;
1604 spin_unlock_irqsave(&systrace_lock);
1607 /* If you registered a process specifically, then you need to dereg it
1608 * specifically. Or just fully stop, which will do it for all. */
1609 int systrace_dereg(bool all, struct proc *p)
1611 spin_lock_irqsave(&systrace_lock);
1613 printk("No longer tracing syscalls for all processes.\n");
1614 systrace_flags &= ~SYSTRACE_ALLPROC;
1616 for (int i = 0; i < MAX_NUM_TRACED; i++) {
1617 if (systrace_procs[i] == p) {
1618 systrace_procs[i] = 0;
1619 printk("No longer tracing syscalls for process %d\n", p->pid);
1623 spin_unlock_irqsave(&systrace_lock);
1627 /* Regardless of locking, someone could be writing into the buffer */
1628 void systrace_print(bool all, struct proc *p)
1630 spin_lock_irqsave(&systrace_lock);
1631 /* if you want to be clever, you could make this start from the earliest
1632 * timestamp and loop around. Careful of concurrent writes. */
1633 for (int i = 0; i < systrace_bufsize; i++)
1634 if (systrace_buffer[i].timestamp)
1635 printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, %08p,"
1636 "%08p) proc: %d core: %d vcore: %d\n",
1637 systrace_buffer[i].timestamp,
1638 systrace_buffer[i].syscallno,
1639 syscall_table[systrace_buffer[i].syscallno].name,
1640 systrace_buffer[i].arg0,
1641 systrace_buffer[i].arg1,
1642 systrace_buffer[i].arg2,
1643 systrace_buffer[i].arg3,
1644 systrace_buffer[i].arg4,
1645 systrace_buffer[i].arg5,
1646 systrace_buffer[i].pid,
1647 systrace_buffer[i].coreid,
1648 systrace_buffer[i].vcoreid);
1649 spin_unlock_irqsave(&systrace_lock);
1652 void systrace_clear_buffer(void)
1654 spin_lock_irqsave(&systrace_lock);
1655 memset(systrace_buffer, 0, sizeof(struct systrace_record) * MAX_SYSTRACES);
1656 spin_unlock_irqsave(&systrace_lock);