1 // Simple command-line kernel monitor useful for
2 // controlling the kernel and exploring the system interactively.
7 #include <arch/console.h>
28 #include <ros/memlayout.h>
29 #include <ros/event.h>
31 #define CMDBUF_SIZE 80 // enough for one VGA text line
33 typedef struct command {
36 // return -1 to force monitor to exit
37 int (*func)(int argc, char **argv, struct hw_trapframe *hw_tf);
40 static command_t commands[] = {
41 { "help", "Display this list of commands", mon_help },
42 { "kerninfo", "Display information about the kernel", mon_kerninfo },
43 { "backtrace", "Dump a backtrace", mon_backtrace },
44 { "bt", "Dump a backtrace", mon_backtrace },
45 { "reboot", "Take a ride to the South Bay", mon_reboot },
46 { "showmapping", "Shows VA->PA mappings", mon_showmapping},
47 { "sm", "Shows VA->PA mappings", mon_sm},
48 { "cpuinfo", "Prints CPU diagnostics", mon_cpuinfo},
49 { "ps", "Prints process list", mon_ps},
50 { "nanwan", "Meet Nanwan!!", mon_nanwan},
51 { "bin_ls", "List files in /bin", mon_bin_ls},
52 { "bin_run", "Create and run a program from /bin", mon_bin_run},
53 { "manager", "Run the manager", mon_manager},
54 { "procinfo", "Show information about processes", mon_procinfo},
55 { "pip", "Shorthand for procinfo pid", mon_pip},
56 { "kill", "Kills a process", mon_kill},
57 { "exit", "Leave the monitor", mon_exit},
58 { "e", "Leave the monitor", mon_exit},
59 { "kfunc", "Run a kernel function directly (!!!)", mon_kfunc},
60 { "notify", "Notify a process. Vcoreid will skip their prefs", mon_notify},
61 { "measure", "Run a specific measurement", mon_measure},
62 { "trace", "Run some tracing functions", mon_trace},
63 { "monitor", "Run the monitor on another core", mon_monitor},
64 { "fs", "Filesystem Diagnostics", mon_fs},
65 { "sh", "Try to run a shell (bash)", mon_shell},
66 { "bash", "Try to run a shell (bash)", mon_shell},
67 { "bb", "Try to run a shell (bash)", mon_shell},
68 { "alarm", "Alarm Diagnostics", mon_alarm},
69 { "msr", "read/write msr: msr msr [value]", mon_msr},
70 { "db", "Misc debugging", mon_db},
71 { "px", "Toggle printx", mon_px},
72 { "kpfret", "Attempt to idle after a kernel fault", mon_kpfret},
73 { "ks", "Kernel scheduler hacks", mon_ks},
74 { "coreinfo", "Print diagnostics for a core", mon_coreinfo},
76 #define NCOMMANDS (sizeof(commands)/sizeof(commands[0]))
78 /***** Implementations of basic kernel monitor commands *****/
80 int mon_help(int argc, char **argv, struct hw_trapframe *hw_tf)
84 for (i = 0; i < NCOMMANDS; i++)
85 cprintf("%s - %s\n", commands[i].name, commands[i].desc);
89 int mon_ps(int argc, char** argv, struct hw_trapframe *hw_tf)
95 int mon_kerninfo(int argc, char **argv, struct hw_trapframe *hw_tf)
97 extern char _start[], etext[], end[];
99 cprintf("Special kernel symbols:\n");
100 cprintf(" _start %016x (virt) %016x (phys)\n", _start, (uintptr_t)(_start - KERNBASE));
101 cprintf(" etext %016x (virt) %016x (phys)\n", etext, (uintptr_t)(etext - KERNBASE));
102 cprintf(" end %016x (virt) %016x (phys)\n", end, (uintptr_t)(end - KERNBASE));
103 cprintf("Kernel executable memory footprint: %dKB\n",
104 (uint32_t)(end-_start+1023)/1024);
108 static int __backtrace(int argc, char **argv, struct hw_trapframe *hw_tf)
116 printk("Need either no arguments, or two (PC and FP) in hex\n");
119 pc = strtol(argv[1], 0, 16);
120 fp = strtol(argv[2], 0, 16);
121 printk("Backtrace from instruction %p, with frame pointer %p\n", pc, fp);
122 backtrace_frame(pc, fp);
126 int mon_backtrace(int argc, char **argv, struct hw_trapframe *hw_tf)
128 return __backtrace(argc, argv, hw_tf);
131 int mon_reboot(int argc, char **argv, struct hw_trapframe *hw_tf)
133 cprintf("[Scottish Accent]: She's goin' down, Cap'n!\n");
136 // really, should never see this
137 cprintf("Sigh....\n");
141 static int __showmapping(int argc, char **argv, struct hw_trapframe *hw_tf)
149 printk("Shows virtual -> physical mappings for a virt addr range.\n");
150 printk("Usage: showmapping PID START_ADDR [END_ADDR]\n");
151 printk(" PID == 0 for the boot pgdir\n");
154 pid = strtol(argv[1], 0, 10);
160 printk("No proc with pid %d\n", pid);
163 pgdir = p->env_pgdir;
165 start = ROUNDDOWN(strtol(argv[2], 0, 16), PGSIZE);
166 size = (argc == 3) ? 1 : strtol(argv[3], 0, 16) - start;
167 if (size/PGSIZE > 512) {
168 cprintf("Not going to do this for more than 512 items\n");
171 show_mapping(pgdir, start, size);
175 int mon_showmapping(int argc, char **argv, struct hw_trapframe *hw_tf)
177 return __showmapping(argc, argv, hw_tf);
180 int mon_sm(int argc, char **argv, struct hw_trapframe *hw_tf)
182 return __showmapping(argc, argv, hw_tf);
185 static spinlock_t print_info_lock = SPINLOCK_INITIALIZER_IRQSAVE;
187 static void print_info_handler(struct hw_trapframe *hw_tf, void *data)
189 uint64_t tsc = read_tsc();
191 spin_lock_irqsave(&print_info_lock);
192 cprintf("----------------------------\n");
193 cprintf("This is Core %d\n", core_id());
194 cprintf("Timestamp = %lld\n", tsc);
196 cprintf("Hardware core %d\n", hw_core_id());
197 cprintf("MTRR_DEF_TYPE = 0x%08x\n", read_msr(IA32_MTRR_DEF_TYPE));
198 cprintf("MTRR Phys0 Base = 0x%016llx, Mask = 0x%016llx\n",
199 read_msr(0x200), read_msr(0x201));
200 cprintf("MTRR Phys1 Base = 0x%016llx, Mask = 0x%016llx\n",
201 read_msr(0x202), read_msr(0x203));
202 cprintf("MTRR Phys2 Base = 0x%016llx, Mask = 0x%016llx\n",
203 read_msr(0x204), read_msr(0x205));
204 cprintf("MTRR Phys3 Base = 0x%016llx, Mask = 0x%016llx\n",
205 read_msr(0x206), read_msr(0x207));
206 cprintf("MTRR Phys4 Base = 0x%016llx, Mask = 0x%016llx\n",
207 read_msr(0x208), read_msr(0x209));
208 cprintf("MTRR Phys5 Base = 0x%016llx, Mask = 0x%016llx\n",
209 read_msr(0x20a), read_msr(0x20b));
210 cprintf("MTRR Phys6 Base = 0x%016llx, Mask = 0x%016llx\n",
211 read_msr(0x20c), read_msr(0x20d));
212 cprintf("MTRR Phys7 Base = 0x%016llx, Mask = 0x%016llx\n",
213 read_msr(0x20e), read_msr(0x20f));
215 cprintf("----------------------------\n");
216 spin_unlock_irqsave(&print_info_lock);
219 static bool print_all_info(void)
221 cprintf("\nCORE 0 asking all cores to print info:\n");
222 smp_call_function_all(print_info_handler, NULL, 0);
223 cprintf("\nDone!\n");
227 int mon_cpuinfo(int argc, char **argv, struct hw_trapframe *hw_tf)
229 cprintf("Number of Cores detected: %d\n", num_cores);
230 cprintf("Calling CPU's ID: 0x%08x\n", core_id());
233 smp_call_function_self(print_info_handler, NULL, 0);
235 smp_call_function_single(strtol(argv[1], 0, 10),
236 print_info_handler, NULL, 0);
240 int mon_manager(int argc, char** argv, struct hw_trapframe *hw_tf)
243 panic("should never get here");
247 int mon_nanwan(int argc, char **argv, struct hw_trapframe *hw_tf)
249 /* Borrowed with love from http://www.geocities.com/SoHo/7373/zoo.htm
250 * (http://www.ascii-art.com/). Slightly modified to make it 25 lines tall.
253 printk(" .-. .-.\n");
254 printk(" | \\/ |\n");
255 printk(" /, ,_ `'-.\n");
256 printk(" .-|\\ /`\\ '. \n");
257 printk(" .' 0/ | 0\\ \\_ `\". \n");
258 printk(" .-' _,/ '--'.'|#''---'\n");
259 printk(" `--' | / \\#\n");
260 printk(" | / \\#\n");
261 printk(" \\ ;|\\ .\\#\n");
262 printk(" |' ' // \\ ::\\# \n");
263 printk(" \\ /` \\ ':\\#\n");
264 printk(" `\"` \\.. \\#\n");
265 printk(" \\::. \\#\n");
266 printk(" \\:: \\#\n");
267 printk(" \\' .:\\#\n");
268 printk(" \\ :::\\#\n");
269 printk(" \\ '::\\#\n");
271 printk(" \\:. \\#\n");
272 printk(" \\:: \\#\n");
273 printk(" \\' .\\#\n");
274 printk(" jgs \\ ::\\#\n");
279 int mon_bin_ls(int argc, char **argv, struct hw_trapframe *hw_tf)
281 struct dirent dir = {0};
282 struct file *bin_dir;
285 bin_dir = do_file_open("/bin", O_READ, 0);
287 printk("No /bin directory!\n");
290 printk("Files in /bin:\n-------------------------------\n");
292 retval = bin_dir->f_op->readdir(bin_dir, &dir);
293 printk("%s\n", dir.d_name);
294 } while (retval == 1);
295 kref_put(&bin_dir->f_kref);
299 int mon_bin_run(int argc, char **argv, struct hw_trapframe *hw_tf)
302 printk("Usage: bin_run FILENAME\n");
305 struct file *program;
307 char buf[5 + MAX_FILENAME_SZ + 1] = "/bin/"; /* /bin/ + max + \0 */
309 strlcpy(buf, "/bin/", sizeof(buf));
310 if (strlcat(buf, argv[1], sizeof(buf)) > sizeof(buf)) {
311 printk("Filename '%s' too long!\n", argv[1]);
314 program = do_file_open(buf, O_READ, 0);
316 printk("No such program!\n");
319 char **p_argv = kmalloc(sizeof(char*) * argc, 0); /* bin_run's argc */
320 for (int i = 0; i < argc - 1; i++)
321 p_argv[i] = argv[i + 1];
322 p_argv[argc - 1] = 0;
323 /* super ugly: we need to stash current, so that proc_create doesn't pick up
324 * on random processes running here and assuming they are the parent */
325 struct proc *old_cur = current;
327 struct proc *p = proc_create(program, p_argv, NULL);
331 proc_decref(p); /* let go of the reference created in proc_create() */
332 kref_put(&program->f_kref);
333 /* Make a scheduling decision. You might not get the process you created,
334 * in the event there are others floating around that are runnable */
336 /* want to idle, so we un the process we just selected. this is a bit
337 * hackish, but so is the monitor. */
343 int mon_procinfo(int argc, char **argv, struct hw_trapframe *hw_tf)
348 printk("Usage: procinfo OPTION\n");
349 printk("\tall: show all active pids\n");
350 printk("\tpid NUM: show a lot of info for proc NUM\n");
351 printk("\tunlock: unlock the lock for the ADDR (OMG!!!)\n");
352 printk("\tkill NUM: destroy proc NUM\n");
355 if (!strcmp(argv[1], "all")) {
357 } else if (!strcmp(argv[1], "pid")) {
359 printk("Give me a pid number.\n");
363 verbosity = strtol(argv[3], 0, 0);
364 print_proc_info(strtol(argv[2], 0, 0), verbosity);
365 } else if (!strcmp(argv[1], "unlock")) {
367 printk("Gimme lock address! Me want lock address!.\n");
370 spinlock_t *lock = (spinlock_t*)strtol(argv[2], 0, 16);
372 printk("Null address...\n");
376 } else if (!strcmp(argv[1], "kill")) {
378 printk("Give me a pid number.\n");
381 struct proc *p = pid2proc(strtol(argv[2], 0, 0));
383 printk("No such proc\n");
389 printk("Bad option\n");
395 int mon_pip(int argc, char **argv, struct hw_trapframe *hw_tf)
400 printk("Give me a pid number.\n");
404 verbosity = strtol(argv[2], 0, 0);
405 print_proc_info(strtol(argv[1], 0, 0), verbosity);
409 int mon_kill(int argc, char **argv, struct hw_trapframe *hw_tf)
414 printk("Usage: kill PID\n");
417 p = pid2proc(strtol(argv[1], 0, 0));
419 printk("No such proc\n");
422 p->exitcode = 1; /* typical EXIT_FAILURE */
428 int mon_exit(int argc, char **argv, struct hw_trapframe *hw_tf)
433 int mon_kfunc(int argc, char **argv, struct hw_trapframe *hw_tf)
436 long (*func)(void *arg, ...);
439 printk("Usage: kfunc FUNCTION [arg1] [arg2] [etc]\n");
440 printk("Use 0x with hex arguments. Can take 6 args.\n");
443 func = (void*)get_symbol_addr(argv[1]);
445 printk("Function not found.\n");
448 /* Not elegant, but whatever. maybe there's a better syntax, or we can do
449 * it with asm magic. */
451 case 2: /* have to fake one arg */
452 ret = func((void*)0);
454 case 3: /* the real first arg */
455 ret = func((void*)strtol(argv[2], 0, 0));
458 ret = func((void*)strtol(argv[2], 0, 0),
459 strtol(argv[3], 0, 0));
462 ret = func((void*)strtol(argv[2], 0, 0),
463 strtol(argv[3], 0, 0),
464 strtol(argv[4], 0, 0));
467 ret = func((void*)strtol(argv[2], 0, 0),
468 strtol(argv[3], 0, 0),
469 strtol(argv[4], 0, 0),
470 strtol(argv[5], 0, 0));
473 ret = func((void*)strtol(argv[2], 0, 0),
474 strtol(argv[3], 0, 0),
475 strtol(argv[4], 0, 0),
476 strtol(argv[5], 0, 0),
477 strtol(argv[6], 0, 0));
480 ret = func((void*)strtol(argv[2], 0, 0),
481 strtol(argv[3], 0, 0),
482 strtol(argv[4], 0, 0),
483 strtol(argv[5], 0, 0),
484 strtol(argv[6], 0, 0),
485 strtol(argv[7], 0, 0));
488 printk("Bad number of arguments.\n");
491 printk("%s (might have) returned %p\n", argv[1], ret);
495 /* Sending a vcoreid forces an event and an IPI/notification */
496 int mon_notify(int argc, char **argv, struct hw_trapframe *hw_tf)
500 struct event_msg msg = {0};
503 printk("Usage: notify PID NUM [VCOREID]\n");
506 p = pid2proc(strtol(argv[1], 0, 0));
508 printk("No such proc\n");
511 msg.ev_type = strtol(argv[2], 0, 0);
513 vcoreid = strtol(argv[3], 0, 0);
514 /* This will go to the private mbox */
515 post_vcore_event(p, &msg, vcoreid, EVENT_VCORE_PRIVATE);
516 proc_notify(p, vcoreid);
518 /* o/w, try and do what they want */
519 send_kernel_event(p, &msg, 0);
525 /* Micro-benchmarky Measurements. This is really fragile code that probably
526 * won't work perfectly, esp as the kernel evolves. */
527 int mon_measure(int argc, char **argv, struct hw_trapframe *hw_tf)
529 uint64_t begin = 0, diff = 0;
530 uint32_t end_refcnt = 0;
533 printk("Usage: measure OPTION\n");
534 printk("\tkill PID : kill proc PID\n");
535 printk("\tpreempt PID : preempt proc PID (no delay)\n");
536 printk("\tpreempt PID [pcore] : preempt PID's pcore (no delay)\n");
537 printk("\tpreempt-warn PID : warn-preempt proc PID (pending)\n");
538 printk("\tpreempt-warn PID [pcore] : warn-preempt proc PID's pcore\n");
539 printk("\tpreempt-raw PID : raw-preempt proc PID\n");
540 printk("\tpreempt-raw PID [pcore] : raw-preempt proc PID's pcore\n");
543 if (!strcmp(argv[1], "kill")) {
545 printk("Give me a pid number.\n");
548 struct proc *p = pid2proc(strtol(argv[2], 0, 0));
550 printk("No such proc\n");
553 begin = start_timing();
554 #ifdef CONFIG_APPSERVER
555 printk("Warning: this will be inaccurate due to the appserver.\n");
556 end_refcnt = kref_refcnt(&p->p_kref) - p->procinfo->num_vcores - 1;
557 #endif /* CONFIG_APPSERVER */
560 #ifdef CONFIG_APPSERVER
561 /* Won't be that accurate, since it's not actually going through the
562 * __proc_free() path. */
563 spin_on(kref_refcnt(&p->p_kref) != end_refcnt);
565 /* this is a little ghetto. it's not fully free yet, but we are also
566 * slowing it down by messing with it, esp with the busy waiting on a
567 * hyperthreaded core. */
569 #endif /* CONFIG_APPSERVER */
570 /* No noticeable difference using stop_timing instead of read_tsc() */
571 diff = stop_timing(begin);
572 } else if (!strcmp(argv[1], "preempt")) {
574 printk("Give me a pid number.\n");
577 struct proc *p = pid2proc(strtol(argv[2], 0, 0));
579 printk("No such proc\n");
582 if (argc == 4) { /* single core being preempted, warned but no delay */
583 uint32_t pcoreid = strtol(argv[3], 0, 0);
584 begin = start_timing();
585 if (proc_preempt_core(p, pcoreid, 1000000)) {
586 __sched_put_idle_core(p, pcoreid);
587 /* done when unmapped (right before abandoning) */
588 spin_on(p->procinfo->pcoremap[pcoreid].valid);
590 printk("Core %d was not mapped to proc\n", pcoreid);
592 diff = stop_timing(begin);
593 } else { /* preempt all cores, warned but no delay */
594 end_refcnt = kref_refcnt(&p->p_kref) - p->procinfo->num_vcores;
595 begin = start_timing();
596 proc_preempt_all(p, 1000000);
597 /* a little ghetto, implies no one is using p */
598 spin_on(kref_refcnt(&p->p_kref) != end_refcnt);
599 diff = stop_timing(begin);
602 } else if (!strcmp(argv[1], "preempt-warn")) {
604 printk("Give me a pid number.\n");
607 struct proc *p = pid2proc(strtol(argv[2], 0, 0));
609 printk("No such proc\n");
612 printk("Careful: if this hangs, then the process isn't responding.\n");
613 if (argc == 4) { /* single core being preempted-warned */
614 uint32_t pcoreid = strtol(argv[3], 0, 0);
615 spin_lock(&p->proc_lock);
616 uint32_t vcoreid = p->procinfo->pcoremap[pcoreid].vcoreid;
617 if (!p->procinfo->pcoremap[pcoreid].valid) {
618 printk("Pick a mapped pcore\n");
619 spin_unlock(&p->proc_lock);
622 begin = start_timing();
623 __proc_preempt_warn(p, vcoreid, 1000000); // 1 sec
624 spin_unlock(&p->proc_lock);
625 /* done when unmapped (right before abandoning) */
626 spin_on(p->procinfo->pcoremap[pcoreid].valid);
627 diff = stop_timing(begin);
628 } else { /* preempt-warn all cores */
629 printk("Warning, this won't work if they can't yield their "
630 "last vcore, will stop at 1!\n");
631 spin_lock(&p->proc_lock);
632 begin = start_timing();
633 __proc_preempt_warnall(p, 1000000);
634 spin_unlock(&p->proc_lock);
635 /* target cores do the unmapping / changing of the num_vcores */
636 spin_on(p->procinfo->num_vcores > 1);
637 diff = stop_timing(begin);
640 } else if (!strcmp(argv[1], "preempt-raw")) {
642 printk("Give me a pid number.\n");
645 struct proc *p = pid2proc(strtol(argv[2], 0, 0));
647 printk("No such proc\n");
650 if (argc == 4) { /* single core preempted, no warning or waiting */
651 uint32_t pcoreid = strtol(argv[3], 0, 0);
652 spin_lock(&p->proc_lock);
653 if (!p->procinfo->pcoremap[pcoreid].valid) {
654 printk("Pick a mapped pcore\n");
655 spin_unlock(&p->proc_lock);
658 begin = start_timing();
659 __proc_preempt_core(p, pcoreid);
660 if (!p->procinfo->num_vcores)
661 __proc_set_state(p, PROC_RUNNABLE_M);
662 spin_unlock(&p->proc_lock);
663 /* ghetto, since the ksched should be calling all of this */
664 __sched_put_idle_core(p, pcoreid);
665 /* done when unmapped (right before abandoning) */
666 spin_on(p->procinfo->pcoremap[pcoreid].valid);
667 diff = stop_timing(begin);
668 } else { /* preempt all cores, no warning or waiting */
669 spin_lock(&p->proc_lock);
670 uint32_t pc_arr[p->procinfo->num_vcores];
671 uint32_t num_revoked;
672 end_refcnt = kref_refcnt(&p->p_kref) - p->procinfo->num_vcores;
673 begin = start_timing();
674 num_revoked = __proc_preempt_all(p, pc_arr);
675 __proc_set_state(p, PROC_RUNNABLE_M);
676 spin_unlock(&p->proc_lock);
678 __sched_put_idle_cores(p, pc_arr, num_revoked);
679 /* a little ghetto, implies no one else is using p */
680 spin_on(kref_refcnt(&p->p_kref) != end_refcnt);
681 diff = stop_timing(begin);
685 printk("Bad option\n");
688 printk("[Tired Giraffe Accent] Took %llu usec (%llu nsec) to finish.\n",
689 tsc2usec(diff), tsc2nsec(diff));
693 static bool mon_verbose_trace = FALSE;
694 static DEFINE_PERCPU(bool, mon_nmi_trace);
696 static void emit_hwtf_backtrace(struct hw_trapframe *hw_tf)
700 if (mon_verbose_trace) {
701 print_trapframe(hw_tf);
702 backtrace_hwtf(hw_tf);
704 fn_name = get_fn_name(get_hwtf_pc(hw_tf));
705 printk("Core %d is at %p (%s)\n", core_id(), get_hwtf_pc(hw_tf),
710 static void emit_vmtf_backtrace(struct vm_trapframe *vm_tf)
712 if (mon_verbose_trace)
713 print_vmtrapframe(vm_tf);
714 printk("Core %d is at %p\n", core_id(), get_vmtf_pc(vm_tf));
717 /* This is dangerous and could cause a deadlock, since it runs in NMI context.
718 * It's only for monitor debugging, so YMMV. We pass the type since the kernel
719 * doesn't deal in contexts (yet) */
720 void emit_monitor_backtrace(int type, void *tf)
722 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
724 if (!PERCPU_VAR(mon_nmi_trace))
726 /* To prevent a spew of output during a lot of perf NMIs, we'll turn off the
727 * monitor output as soon as any NMI hits our core. */
728 PERCPU_VAR(mon_nmi_trace) = FALSE;
729 /* Temporarily disable deadlock detection when we print. We could
730 * deadlock if we were printing when we NMIed. */
731 pcpui->__lock_checking_enabled--;
732 if (type == ROS_HW_CTX)
733 emit_hwtf_backtrace((struct hw_trapframe*)tf);
735 emit_vmtf_backtrace((struct vm_trapframe*)tf);
736 print_kmsgs(core_id());
737 pcpui->__lock_checking_enabled++;
741 int mon_trace(int argc, char **argv, struct hw_trapframe *hw_tf)
745 printk("Usage: trace OPTION\n");
746 printk("\tsyscall start [silent (0 or non-zero, NOT the word silent)] [pid]: starts tracing\n");
747 printk("\tsyscall stop: stops tracing.\n");
748 printk("\tcoretf COREID: prints PC, -1 for all cores, verbose => TF\n");
749 printk("\tpcpui [type [coreid]]: runs pcpui trace ring handlers\n");
750 printk("\tpcpui-reset [noclear]: resets/clears pcpui trace ring\n");
751 printk("\tverbose: toggles verbosity, depends on trace command\n");
754 if (!strcmp(argv[1], "syscall")) {
756 printk("Need a start or stop.\n");
759 if (!strcmp(argv[2], "start")) {
760 systrace_loud = TRUE;
761 } else if (!strcmp(argv[2], "stop")) {
762 systrace_loud = FALSE;
764 printk("Need a start or stop.\n");
767 } else if (!strcmp(argv[1], "coretf")) {
769 printk("Need a coreid, fool.\n");
772 core = strtol(argv[2], 0, 0);
774 printk("Sending NMIs to all cores:\n");
775 for (int i = 0; i < num_cores; i++) {
776 _PERCPU_VAR(mon_nmi_trace, i) = TRUE;
781 printk("Sending NMI core %d:\n", core);
782 if (core >= num_cores) {
783 printk("No such core! Maybe it's in another cell...\n");
786 _PERCPU_VAR(mon_nmi_trace, core) = TRUE;
790 } else if (!strcmp(argv[1], "pcpui")) {
791 int pcpui_type, pcpui_coreid;
793 pcpui_type = strtol(argv[2], 0, 0);
796 printk("\nRunning PCPUI Trace Ring handlers for type %d\n", pcpui_type);
798 pcpui_coreid = strtol(argv[3], 0, 0);
799 pcpui_tr_foreach(pcpui_coreid, pcpui_type);
801 pcpui_tr_foreach_all(pcpui_type);
803 } else if (!strcmp(argv[1], "pcpui-reset")) {
805 printk("\nResetting all PCPUI Trace Rings\n");
806 pcpui_tr_reset_all();
808 printk("\nResetting and clearing all PCPUI Trace Rings\n");
809 pcpui_tr_reset_and_clear_all();
811 } else if (!strcmp(argv[1], "verbose")) {
812 if (mon_verbose_trace) {
813 printk("Turning trace verbosity off\n");
814 mon_verbose_trace = FALSE;
816 printk("Turning trace verbosity on\n");
817 mon_verbose_trace = TRUE;
819 } else if (!strcmp(argv[1], "opt2")) {
821 printk("ERRRRRRRRRR.\n");
824 print_proc_info(strtol(argv[2], 0, 0), 0);
826 printk("Bad option\n");
832 int mon_monitor(int argc, char **argv, struct hw_trapframe *hw_tf)
835 printk("Usage: monitor COREID\n");
838 uint32_t core = strtol(argv[1], 0, 0);
839 if (core >= num_cores) {
840 printk("No such core! Maybe it's in another cell...\n");
843 send_kernel_message(core, __run_mon, 0, 0, 0, KMSG_ROUTINE);
847 /***** Kernel monitor command interpreter *****/
849 #define WHITESPACE "\t\r\n "
853 int onecmd(int argc, char *argv[], struct hw_trapframe *hw_tf) {
857 for (i = 0; i < NCOMMANDS; i++) {
858 if (strcmp(argv[0], commands[i].name) == 0)
859 return commands[i].func(argc, argv, hw_tf);
864 void __run_mon(uint32_t srcid, long a0, long a1, long a2)
869 static int runcmd(char *real_buf, struct hw_trapframe *hw_tf) {
870 char * buf = real_buf;
875 // Parse the command buffer into whitespace-separated arguments
880 while (*buf && strchr(WHITESPACE, *buf))
885 // save and scan past next arg
886 if (argc == MAXARGS-1) {
887 cprintf("Too many arguments (max %d)\n", MAXARGS);
890 //This will get fucked at runtime..... in the ASS
892 while (*buf && !strchr(WHITESPACE, *buf))
897 // Lookup and invoke the command
900 for (i = 0; i < NCOMMANDS; i++) {
901 if (strcmp(argv[0], commands[i].name) == 0)
902 return commands[i].func(argc, argv, hw_tf);
904 cprintf("Unknown command '%s'\n", argv[0]);
908 void monitor(struct hw_trapframe *hw_tf)
910 #define MON_CMD_LENGTH 256
911 char buf[MON_CMD_LENGTH];
913 int coreid = core_id_early();
915 /* they are always disabled, since we have this irqsave lock */
916 if (irq_is_enabled())
917 printk("Entering Nanwan's Dungeon on Core %d (Ints on):\n", coreid);
919 printk("Entering Nanwan's Dungeon on Core %d (Ints off):\n", coreid);
920 printk("Type 'help' for a list of commands.\n");
923 print_trapframe(hw_tf);
926 /* on occasion, the kernel monitor can migrate (like if you run
927 * something that blocks / syncs and wakes up on another core) */
929 cnt = readline(buf, MON_CMD_LENGTH, "ROS(Core %d)> ", core_id_early());
932 if (runcmd(buf, hw_tf) < 0)
938 static void pm_flusher(void *unused)
940 struct super_block *sb;
942 unsigned long nr_pages;
944 /* could also put the delay between calls, or even within remove, during the
946 printk("GIANT WARNING: the pm_flusher is running and will never stop!\n");
948 kthread_usleep(5000);
949 TAILQ_FOREACH(sb, &super_blocks, s_list) {
950 TAILQ_FOREACH(inode, &sb->s_inodes, i_sb_list) {
951 nr_pages = ROUNDUP(inode->i_size, PGSIZE) >> PGSHIFT;
953 pm_remove_contig(inode->i_mapping, 0, nr_pages);
959 int mon_fs(int argc, char **argv, struct hw_trapframe *hw_tf)
961 /* this assumes one mounted FS at the NS root */
962 struct super_block *sb;
965 struct dentry *dentry;
967 printk("Usage: fs OPTION\n");
968 printk("\topen: show all open files\n");
969 printk("\tinodes: show all inodes\n");
970 printk("\tdentries [lru|prune]: show all dentries, opt LRU/prune\n");
971 printk("\tls DIR: print the dir tree starting with DIR\n");
972 printk("\tpid: proc PID's fs crap placeholder\n");
973 printk("\tpmflusher: start a ktask to keep flushing all PMs\n");
976 if (!strcmp(argv[1], "open")) {
977 printk("Open Files:\n----------------------------\n");
978 TAILQ_FOREACH(sb, &super_blocks, s_list) {
979 printk("Superblock for %s\n", sb->s_name);
980 TAILQ_FOREACH(file, &sb->s_files, f_list)
981 printk("File: %p, %s, Refs: %d, Drefs: %d, Irefs: %d PM: %p\n",
982 file, file_name(file), kref_refcnt(&file->f_kref),
983 kref_refcnt(&file->f_dentry->d_kref),
984 kref_refcnt(&file->f_dentry->d_inode->i_kref),
987 } else if (!strcmp(argv[1], "inodes")) {
988 printk("Mounted FS Inodes:\n----------------------------\n");
989 TAILQ_FOREACH(sb, &super_blocks, s_list) {
990 printk("Superblock for %s\n", sb->s_name);
991 TAILQ_FOREACH(inode, &sb->s_inodes, i_sb_list) {
992 printk("Inode: %p, Refs: %d, Nlinks: %d, Size(B): %d\n",
993 inode, kref_refcnt(&inode->i_kref), inode->i_nlink,
995 TAILQ_FOREACH(dentry, &inode->i_dentry, d_alias)
996 printk("\t%s: Dentry: %p, Refs: %d\n",
997 dentry->d_name.name, dentry,
998 kref_refcnt(&dentry->d_kref));
1001 } else if (!strcmp(argv[1], "dentries")) {
1002 printk("Dentry Cache:\n----------------------------\n");
1003 TAILQ_FOREACH(sb, &super_blocks, s_list) {
1004 printk("Superblock for %s\n", sb->s_name);
1005 printk("DENTRY FLAGS REFCNT NAME\n");
1006 printk("--------------------------------\n");
1008 void print_dcache_entry(void *item, void *opaque)
1010 struct dentry *d_i = (struct dentry*)item;
1011 printk("%p %p %02d %s\n", d_i, d_i->d_flags,
1012 kref_refcnt(&d_i->d_kref), d_i->d_name.name);
1014 hash_for_each(sb->s_dcache, print_dcache_entry, NULL);
1018 if (!strcmp(argv[2], "lru")) {
1019 printk("LRU lists:\n");
1020 TAILQ_FOREACH(sb, &super_blocks, s_list) {
1021 printk("Superblock for %s\n", sb->s_name);
1022 TAILQ_FOREACH(dentry, &sb->s_lru_d, d_lru)
1023 printk("Dentry: %p, Name: %s\n", dentry,
1024 dentry->d_name.name);
1026 } else if (!strcmp(argv[2], "prune")) {
1027 printk("Pruning unused dentries\n");
1028 TAILQ_FOREACH(sb, &super_blocks, s_list)
1029 dcache_prune(sb, FALSE);
1031 } else if (!strcmp(argv[1], "ls")) {
1033 printk("Give me a dir.\n");
1036 if (argv[2][0] != '/') {
1037 printk("Dear fellow giraffe lover, Use absolute paths.\n");
1041 /* whatever. placeholder. */
1042 } else if (!strcmp(argv[1], "pid")) {
1044 printk("Give me a pid number.\n");
1047 /* whatever. placeholder. */
1048 } else if (!strcmp(argv[1], "pmflusher")) {
1049 ktask("pm_flusher", pm_flusher, 0);
1051 printk("Bad option\n");
1057 int mon_shell(int argc, char **argv, struct hw_trapframe *hw_tf)
1059 char *l_argv[2] = {"/bin/bash", "bash"};
1060 return mon_bin_run(2, l_argv, hw_tf);
1063 int mon_alarm(int argc, char **argv, struct hw_trapframe *hw_tf)
1066 printk("Usage: alarm OPTION\n");
1067 printk("\tpcpu: print full alarm tchains from every core\n");
1070 if (!strcmp(argv[1], "pcpu")) {
1071 print_pcpu_chains();
1073 printk("Bad option\n");
1079 static void show_msr(struct hw_trapframe *unused, void *v)
1081 int core = core_id();
1083 uint32_t msr = *(uint32_t *)v;
1084 val = read_msr(msr);
1085 printk("%d: %08x: %016llx\n", core, msr, val);
1093 static void set_msr(struct hw_trapframe *unused, void *v)
1095 int core = core_id();
1097 uint32_t msr = s->msr;
1098 uint64_t val = s->val;
1099 write_msr(msr, val);
1100 val = read_msr(msr);
1101 printk("%d: %08x: %016llx\n", core, msr, val);
1104 int mon_msr(int argc, char **argv, struct hw_trapframe *hw_tf)
1107 cprintf("Not on this architecture\n");
1112 if (argc < 2 || argc > 3) {
1113 printk("Usage: msr register [value]\n");
1116 msr = strtoul(argv[1], 0, 16);
1117 handler_wrapper_t *w;
1118 smp_call_function_all(show_msr, &msr, &w);
1123 /* somewhat bogus on 32 bit. */
1124 val = strtoul(argv[2], 0, 16);
1129 smp_call_function_all(set_msr, &set, &w);
1135 int mon_db(int argc, char **argv, struct hw_trapframe *hw_tf)
1140 printk("Usage: db OPTION\n");
1141 printk("\tsem [PID]: print all semaphore info\n");
1142 printk("\taddr PID 0xADDR: for PID lookup ADDR's file/vmr info\n");
1145 if (!strcmp(argv[1], "sem")) {
1147 pid = strtol(argv[2], 0, 0);
1148 print_all_sem_info(pid);
1149 } else if (!strcmp(argv[1], "addr")) {
1151 printk("Usage: db addr PID 0xADDR\n");
1154 debug_addr_pid(strtol(argv[2], 0, 10), strtol(argv[3], 0, 16));
1156 printk("Bad option\n");
1162 int mon_px(int argc, char **argv, struct hw_trapframe *hw_tf)
1165 printk("Printxing is now %sabled\n", printx_on ? "en" : "dis");
1169 /* Super hack. Given a kernel hw_tf, we hack the RIP to smp_idle, then return
1170 * to it. Any locks or other stuff being done is completely lost, so you could
1171 * deadlock. This gets out of the "we're totall screwed, but don't want to
1172 * reboot right now", typically caused by screw-ups from the monitor. */
1173 int mon_kpfret(int argc, char **argv, struct hw_trapframe *hw_tf)
1175 struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
1177 /* if monitor had a TF, try to use that */
1180 printk("Usage: kpfret HW_TF\n");
1183 /* the hw_tf passed in is the one we got from monitor, which is 0 from
1185 hw_tf = (struct hw_trapframe*)strtol(argv[1], 0, 16);
1188 if (!in_kernel(hw_tf)) {
1189 printk("hw_tf %p was not a kernel tf!\n", hw_tf);
1194 hw_tf->tf_rip = (uintptr_t)smp_idle;
1195 dec_ktrap_depth(pcpui);
1197 asm volatile("mov %0, %%rsp;"
1198 "addq $0x10, %%rsp;"
1214 "addq $0x10, %%rsp;"
1219 printk("KPF return not supported\n");
1221 #endif /* CONFIG_X86 */
1224 int mon_ks(int argc, char **argv, struct hw_trapframe *hw_tf)
1228 printk("Usage: ks OPTION\n");
1229 printk("\tidles: show idle core map\n");
1230 printk("\tdiag: scheduler diagnostic report\n");
1231 printk("\tresources: show resources wanted/granted for all procs\n");
1232 printk("\tsort: sorts the idlecoremap, 1..n\n");
1233 printk("\tnc PCOREID: sets the next CG core allocated\n");
1236 if (!strcmp(argv[1], "idles")) {
1237 print_idle_core_map();
1238 } else if (!strcmp(argv[1], "diag")) {
1240 } else if (!strcmp(argv[1], "resources")) {
1241 print_all_resources();
1242 } else if (!strcmp(argv[1], "sort")) {
1244 } else if (!strcmp(argv[1], "nc")) {
1246 printk("Need a pcore number.\n");
1249 next_core_to_alloc(strtol(argv[2], 0, 0));
1251 printk("Bad option %s\n", argv[1]);
1257 /* Prints info about a core. Optional first arg == coreid. */
1258 int mon_coreinfo(int argc, char **argv, struct hw_trapframe *hw_tf)
1260 struct per_cpu_info *pcpui;
1261 struct kthread *kth;
1262 int coreid = core_id();
1265 coreid = strtol(argv[1], 0, 0);
1266 pcpui = &per_cpu_info[coreid];
1267 printk("Core %d:\n\tcur_proc %d\n\towning proc %d, owning vc %d\n",
1268 coreid, pcpui->cur_proc ? pcpui->cur_proc->pid : 0,
1269 pcpui->owning_proc ? pcpui->owning_proc->pid : 0,
1270 pcpui->owning_vcoreid != 0xdeadbeef ? pcpui->owning_vcoreid : 0);
1271 kth = pcpui->cur_kthread;
1273 /* kth->proc is only used when the kthread is sleeping. when it's
1274 * running, we care about cur_proc. if we're here, proc should be 0
1275 * unless the kth is concurrently sleeping (we called this remotely) */
1276 printk("\tkthread %p (%s), sysc %p (%d)\n", kth, kth->name,
1277 kth->sysc, kth->sysc ? kth->sysc->num : -1);
1279 /* Can happen during early boot */
1280 printk("\tNo kthread!\n");