Fixes gcc 4.6.1 incompatibility with old x86 asm
[akaros.git] / kern / src / monitor.c
index aafe6de..ea6fd8b 100644 (file)
@@ -18,7 +18,6 @@
 #include <pmap.h>
 #include <kdebug.h>
 #include <testing.h>
-#include <kfs.h>
 #include <manager.h>
 #include <schedule.h>
 #include <resource.h>
 #include <syscall.h>
 #include <kmalloc.h>
 #include <elf.h>
+#include <event.h>
+#include <trap.h>
 
-#include <ros/timer.h>
+#include <ros/time.h>
 #include <ros/memlayout.h>
+#include <ros/event.h>
 
 #define CMDBUF_SIZE    80      // enough for one VGA text line
 
@@ -57,8 +59,11 @@ static command_t (RO commands)[] = {
        { "kfunc", "Run a kernel function directly (!!!)", mon_kfunc},
        { "notify", "Notify a process.  Vcoreid will skip their prefs", mon_notify},
        { "measure", "Run a specific measurement", mon_measure},
-       { "trace", "Run a specific measurement", mon_trace},
+       { "trace", "Run some tracing functions", mon_trace},
        { "monitor", "Run the monitor on another core", mon_monitor},
+       { "fs", "Filesystem Diagnostics", mon_fs},
+       { "bb", "Try to run busybox (ash)", mon_bb},
+       { "alarm", "Alarm Diagnostics", mon_alarm},
 };
 #define NCOMMANDS (sizeof(commands)/sizeof(commands[0]))
 
@@ -84,10 +89,10 @@ int mon_kerninfo(int argc, char **argv, trapframe_t *tf)
        extern char (RO SNT _start)[], (RO SNT etext)[], (RO SNT edata)[], (RO SNT end)[];
 
        cprintf("Special kernel symbols:\n");
-       cprintf("  _start %08x (virt)  %08x (phys)\n", _start, (uint32_t)(_start - KERNBASE));
-       cprintf("  etext  %08x (virt)  %08x (phys)\n", etext, (uint32_t)(etext - KERNBASE));
-       cprintf("  edata  %08x (virt)  %08x (phys)\n", edata, (uint32_t)(edata - KERNBASE));
-       cprintf("  end    %08x (virt)  %08x (phys)\n", end, (uint32_t)(end - KERNBASE));
+       cprintf("  _start %016x (virt)  %016x (phys)\n", _start, (uintptr_t)(_start - KERNBASE));
+       cprintf("  etext  %016x (virt)  %016x (phys)\n", etext, (uintptr_t)(etext - KERNBASE));
+       cprintf("  edata  %016x (virt)  %016x (phys)\n", edata, (uintptr_t)(edata - KERNBASE));
+       cprintf("  end    %016x (virt)  %016x (phys)\n", end, (uintptr_t)(end - KERNBASE));
        cprintf("Kernel executable memory footprint: %dKB\n",
                (uint32_t)(end-_start+1023)/1024);
        return 0;
@@ -257,7 +262,7 @@ int mon_bin_ls(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        struct file *bin_dir;
        int retval = 0;
 
-       bin_dir = path_to_file("/bin");
+       bin_dir = do_file_open("/bin", 0, 0);
        if (!bin_dir) {
                printk("No /bin directory!\n");
                return 1;
@@ -266,45 +271,40 @@ int mon_bin_ls(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        do {
                retval = bin_dir->f_op->readdir(bin_dir, &dir); 
                printk("%s\n", dir.d_name);
-               dir.d_off++;
        } while (retval == 1);
-       /* TODO: (REF) need to dealloc when this hits 0, atomic/concurrent/etc */
-       atomic_dec(&bin_dir->f_refcnt);
-       assert(!bin_dir->f_refcnt);
-       bin_dir->f_op->release(bin_dir->f_inode, bin_dir);
+       kref_put(&bin_dir->f_kref);
        return 0;
 }
 
 int mon_bin_run(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
-       if (argc != 2) {
-               printk("Usage: kfs_run FILENAME\n");
+       if (argc < 2) {
+               printk("Usage: bin_run FILENAME\n");
                return 1;
        }
        struct file *program;
        int retval = 0;
        char buf[6 + MAX_FILENAME_SZ] = "/bin/";        /* /bin/ + max + \0 */
        strncpy(buf + 5, argv[1], MAX_FILENAME_SZ);
-       program = path_to_file(buf);
+       program = do_file_open(buf, 0, 0);
        if (!program) {
                printk("No such program!\n");
                return 1;
        }
-       /* TODO: push this into proc_create */
-       struct proc *p = proc_create(NULL, 0);
-       char* p_argv[] = {0, 0, 0};
-       char* p_envp[] = {"LD_LIBRARY_PATH=/lib",0};
-       p_argv[0] = file_name(program);
-       procinfo_pack_args(p->procinfo, p_argv, p_envp);
-       assert(load_elf(p, program) == 0);
+       char **p_argv = kmalloc(sizeof(char*) * argc, 0);       /* bin_run's argc */
+       for (int i = 0; i < argc - 1; i++)
+               p_argv[i] = argv[i + 1];
+       p_argv[argc - 1] = 0;
+       char *p_envp[] = {"LD_LIBRARY_PATH=/lib", 0};
+       struct proc *p = proc_create(program, p_argv, p_envp);
+       kfree(p_argv);
 
        spin_lock(&p->proc_lock);
        __proc_set_state(p, PROC_RUNNABLE_S);
        schedule_proc(p);
        spin_unlock(&p->proc_lock);
-       proc_decref(p, 1); /* let go of the reference created in proc_create() */
-       /* TODO: (REF) need to dealloc when this hits 0, atomic/concurrent/etc */
-       atomic_dec(&program->f_refcnt);
+       proc_decref(p); /* let go of the reference created in proc_create() */
+       kref_put(&program->f_kref);
        /* Should never return from schedule (env_pop in there) also note you may
         * not get the process you created, in the event there are others floating
         * around that are runnable */
@@ -361,7 +361,7 @@ int mon_procinfo(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                        return 1;
                }
                proc_destroy(p);
-               proc_decref(p, 1);
+               proc_decref(p);
        } else {
                printk("Bad option\n");
                return 1;
@@ -439,11 +439,12 @@ int mon_kfunc(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        return 0;
 }
 
+/* Sending a vcoreid forces an event and an IPI/notification */
 int mon_notify(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
        struct proc *p;
-       unsigned int num;
        uint32_t vcoreid;
+       struct event_msg msg = {0};
 
        if (argc < 3) {
                printk("Usage: notify PID NUM [VCOREID]\n");
@@ -454,22 +455,24 @@ int mon_notify(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                printk("No such proc\n");
                return 1;
        }
-       num = strtol(argv[2], 0, 0);
+       msg.ev_type = strtol(argv[2], 0, 0);
        if (argc == 4) {
                vcoreid = strtol(argv[3], 0, 0);
-               do_notify(p, vcoreid, num, 0);
+               post_vcore_event(p, &msg, vcoreid);
+               proc_notify(p, vcoreid);
        } else {
-               proc_notify(p, num, 0);
+               /* o/w, try and do what they want */
+               send_kernel_event(p, &msg, 0);
        }
-       proc_decref(p, 1);
+       proc_decref(p);
        return 0;
 }
 
-/* Micro-benchmarky Measurements */
+/* Micro-benchmarky Measurements.  This is really fragile code that probably
+ * won't work perfectly, esp as the kernel evolves. */
 int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
        uint64_t begin = 0, diff = 0;
-       bool self_ipi_pending = FALSE;
        uint32_t end_refcnt = 0;
 
        if (argc < 2) {
@@ -496,14 +499,14 @@ int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                begin = start_timing();
 #ifdef __CONFIG_APPSERVER__
                printk("Warning: this will be inaccurate due to the appserver.\n");
-               end_refcnt = p->env_refcnt - p->procinfo->num_vcores - 1;
+               end_refcnt = kref_refcnt(&p->p_kref) - p->procinfo->num_vcores - 1;
 #endif /* __CONFIG_APPSERVER__ */
                proc_destroy(p);
-               proc_decref(p, 1);
+               proc_decref(p);
 #ifdef __CONFIG_APPSERVER__
                /* Won't be that accurate, since it's not actually going through the
                 * __proc_free() path. */
-               spin_on(p->env_refcnt != end_refcnt);   
+               spin_on(kref_refcnt(&p->p_kref) != end_refcnt); 
 #else
                /* this is a little ghetto. it's not fully free yet, but we are also
                 * slowing it down by messing with it, esp with the busy waiting on a
@@ -530,14 +533,14 @@ int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                        spin_on(p->procinfo->pcoremap[pcoreid].valid);
                        diff = stop_timing(begin);
                } else { /* preempt all cores, warned but no delay */
-                       end_refcnt = p->env_refcnt - p->procinfo->num_vcores;
+                       end_refcnt = kref_refcnt(&p->p_kref) - p->procinfo->num_vcores;
                        begin = start_timing();
                        proc_preempt_all(p, 1000000);
                        /* a little ghetto, implies no one is using p */
-                       spin_on(p->env_refcnt != end_refcnt);
+                       spin_on(kref_refcnt(&p->p_kref) != end_refcnt);
                        diff = stop_timing(begin);
                }
-               proc_decref(p, 1);
+               proc_decref(p);
        } else if (!strcmp(argv[1], "preempt-warn")) {
                if (argc < 3) {
                        printk("Give me a pid number.\n");
@@ -548,6 +551,7 @@ int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                        printk("No such proc\n");
                        return 1;
                }
+               printk("Careful: if this hangs, then the process isn't responding.\n");
                if (argc == 4) { /* single core being preempted-warned */
                        uint32_t pcoreid = strtol(argv[3], 0, 0);
                        spin_lock(&p->proc_lock);
@@ -574,7 +578,7 @@ int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                        spin_on(p->procinfo->num_vcores > 1);
                        diff = stop_timing(begin);
                }
-               proc_decref(p, 1);
+               proc_decref(p);
        } else if (!strcmp(argv[1], "preempt-raw")) {
                if (argc < 3) {
                        printk("Give me a pid number.\n");
@@ -594,42 +598,41 @@ int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                                return 1;
                        }
                        begin = start_timing();
-                       self_ipi_pending = __proc_preempt_core(p, pcoreid);
+                       __proc_preempt_core(p, pcoreid);
                        spin_unlock(&p->proc_lock);
-                       __proc_kmsg_pending(p, self_ipi_pending);
                        /* done when unmapped (right before abandoning) */
                        spin_on(p->procinfo->pcoremap[pcoreid].valid);
                        diff = stop_timing(begin);
                        /* TODO: (RMS), if num_vcores == 0, RUNNABLE_M, schedule */
                } else { /* preempt all cores, no warning or waiting */
                        spin_lock(&p->proc_lock);
-                       end_refcnt = p->env_refcnt - p->procinfo->num_vcores;
+                       end_refcnt = kref_refcnt(&p->p_kref) - p->procinfo->num_vcores;
                        begin = start_timing();
-                       self_ipi_pending = __proc_preempt_all(p);
+                       __proc_preempt_all(p);
                        /* TODO: (RMS), RUNNABLE_M, schedule */
                        spin_unlock(&p->proc_lock);
-                       __proc_kmsg_pending(p, self_ipi_pending);
                        /* a little ghetto, implies no one else is using p */
-                       spin_on(p->env_refcnt != end_refcnt);
+                       spin_on(kref_refcnt(&p->p_kref) != end_refcnt);
                        diff = stop_timing(begin);
                }
-               proc_decref(p, 1);
+               proc_decref(p);
        } else {
                printk("Bad option\n");
                return 1;
        }
        printk("[Tired Giraffe Accent] Took %llu usec (%llu nsec) to finish.\n",
-              diff * 1000000 / system_timing.tsc_freq,
-              diff * 1000000000 / system_timing.tsc_freq);
+              tsc2usec(diff), tsc2nsec(diff));
        return 0;
 }
 
 int mon_trace(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
+       uint32_t core;
        if (argc < 2) {
                printk("Usage: trace OPTION\n");
                printk("\tsyscall start [silent] [pid]: starts tracing\n");
                printk("\tsyscall stop: stops tracing, prints if it was silent\n");
+               printk("\tcoretf COREID: cause the other core to print its TF (NMI)\n");
                return 1;
        }
        if (!strcmp(argv[1], "syscall")) {
@@ -661,6 +664,18 @@ int mon_trace(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                        systrace_print(TRUE, 0);
                        systrace_clear_buffer();
                }
+       } else if (!strcmp(argv[1], "coretf")) {
+               if (argc != 3) {
+                       printk("Need a coreid, fool.\n");
+                       return 1;
+               }
+               core = strtol(argv[2], 0, 0);
+               if (core >= num_cpus) {
+                       printk("No such core!  Maybe it's in another cell...\n");
+                       return 1;
+               }
+               send_nmi(core);
+               udelay(1000000);
        } else if (!strcmp(argv[1], "opt2")) {
                if (argc != 3) {
                        printk("ERRRRRRRRRR.\n");
@@ -685,10 +700,10 @@ int mon_monitor(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                printk("No such core!  Maybe it's in another cell...\n");
                return 1;
        }
-       void run_mon(struct trapframe *tf, uint32_t srcid, void *a0, void *a1,
-                    void *a2)
+       void run_mon(struct trapframe *tf, uint32_t srcid, long a0, long a1,
+                    long a2)
        {
-               monitor(0); // TODO consider passing the tf
+               monitor(0);
        }
        send_kernel_message(core, run_mon, 0, 0, 0, KMSG_ROUTINE);
        return 0;
@@ -738,11 +753,11 @@ static int runcmd(char *NTS real_buf, trapframe_t *tf) {
        return 0;
 }
 
-void monitor(trapframe_t *tf) {
-       static spinlock_t monitor_lock = SPINLOCK_INITIALIZER;
-       spin_lock_irqsave(&monitor_lock);
-
-       char *buf;
+void monitor(struct trapframe *tf)
+{
+       #define MON_CMD_LENGTH 256
+       char buf[MON_CMD_LENGTH];
+       int cnt;
 
        /* they are always disabled, since we have this irqsave lock */
        if (irq_is_enabled())
@@ -754,14 +769,128 @@ void monitor(trapframe_t *tf) {
        if (tf != NULL)
                print_trapframe(tf);
 
-       spin_unlock_irqsave(&monitor_lock);
        while (1) {
-               spin_lock_irqsave(&monitor_lock);
-               buf = readline("ROS(Core %d)> ", core_id());
-               if (buf != NULL) {
-                       spin_unlock_irqsave(&monitor_lock);
+               cnt = readline(buf, MON_CMD_LENGTH, "ROS(Core %d)> ", core_id());
+               if (cnt > 0) {
+                       buf[cnt] = 0;
                        if (runcmd(buf, tf) < 0)
                                break;
                }
        }
 }
+
+int mon_fs(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+{
+       /* this assumes one mounted FS at the NS root */
+       struct super_block *sb;
+       struct file *file;
+       struct inode *inode;
+       struct dentry *dentry;
+       if (argc < 2) {
+               printk("Usage: fs OPTION\n");
+               printk("\topen: show all open files\n");
+               printk("\tinodes: show all inodes\n");
+               printk("\tdentries [lru|prune]: show all dentries, opt LRU/prune\n");
+               printk("\tls DIR: print the dir tree starting with DIR\n");
+               printk("\tpid: proc PID's fs crap placeholder\n");
+               return 1;
+       }
+       if (!strcmp(argv[1], "open")) {
+               printk("Open Files:\n----------------------------\n");
+               TAILQ_FOREACH(sb, &super_blocks, s_list) {
+                       printk("Superblock for %s\n", sb->s_name);
+                       TAILQ_FOREACH(file, &sb->s_files, f_list)
+                               printk("File: %08p, %s, Refs: %d, Drefs: %d, Irefs: %d\n", file,
+                                      file_name(file), kref_refcnt(&file->f_kref),
+                                      kref_refcnt(&file->f_dentry->d_kref),
+                                      kref_refcnt(&file->f_dentry->d_inode->i_kref));
+               }
+       } else if (!strcmp(argv[1], "inodes")) {
+               printk("Mounted FS Inodes:\n----------------------------\n");
+               TAILQ_FOREACH(sb, &super_blocks, s_list) {
+                       printk("Superblock for %s\n", sb->s_name);
+                       TAILQ_FOREACH(inode, &sb->s_inodes, i_sb_list) {
+                               printk("Inode: %08p, Refs: %d, Nlinks: %d, Size(B): %d\n",
+                                      inode, kref_refcnt(&inode->i_kref), inode->i_nlink,
+                                      inode->i_size);
+                               TAILQ_FOREACH(dentry, &inode->i_dentry, d_alias)
+                                       printk("\t%s: Dentry: %08p, Refs: %d\n",
+                                              dentry->d_name.name, dentry,
+                                              kref_refcnt(&dentry->d_kref));
+                       }
+               }
+       } else if (!strcmp(argv[1], "dentries")) {
+               printk("Dentry Cache:\n----------------------------\n");
+               TAILQ_FOREACH(sb, &super_blocks, s_list) {
+                       printk("Superblock for %s\n", sb->s_name);
+                       printk("DENTRY     FLAGS      REFCNT NAME\n");
+                       printk("--------------------------------\n");
+                       /* Hash helper */
+                       void print_dcache_entry(void *item)
+                       {
+                               struct dentry *d_i = (struct dentry*)item;
+                               printk("%08p %08p %02d     %s\n", d_i, d_i->d_flags,
+                                      kref_refcnt(&d_i->d_kref), d_i->d_name.name);
+                       }
+                       hash_for_each(sb->s_dcache, print_dcache_entry);
+               }
+               if (argc < 3)
+                       return 0;
+               if (!strcmp(argv[2], "lru")) {
+                       printk("LRU lists:\n");
+                       TAILQ_FOREACH(sb, &super_blocks, s_list) {
+                               printk("Superblock for %s\n", sb->s_name);
+                               TAILQ_FOREACH(dentry, &sb->s_lru_d, d_lru)
+                                       printk("Dentry: %08p, Name: %s\n", dentry,
+                                              dentry->d_name.name);
+                       }
+               } else if (!strcmp(argv[2], "prune")) {
+                       printk("Pruning unused dentries\n");
+                       TAILQ_FOREACH(sb, &super_blocks, s_list)
+                               dcache_prune(sb, FALSE);
+               }
+       } else if (!strcmp(argv[1], "ls")) {
+               if (argc != 3) {
+                       printk("Give me a dir.\n");
+                       return 1;
+               }
+               if (argv[2][0] != '/') {
+                       printk("Dear fellow giraffe lover, Use absolute paths.\n");
+                       return 1;
+               }
+               ls_dash_r(argv[2]);
+               /* whatever.  placeholder. */
+       } else if (!strcmp(argv[1], "pid")) {
+               if (argc != 3) {
+                       printk("Give me a pid number.\n");
+                       return 1;
+               }
+               /* whatever.  placeholder. */
+       } else {
+               printk("Bad option\n");
+               return 1;
+       }
+       return 0;
+}
+
+int mon_bb(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+{
+       char *l_argv[3] = {"", "busybox", "ash"};
+       return mon_bin_run(3, l_argv, tf);
+}
+
+int mon_alarm(int argc, char **argv, struct trapframe *tf)
+{
+       if (argc < 2) {
+               printk("Usage: alarm OPTION\n");
+               printk("\tpcpu: print full alarm tchains from every core\n");
+               return 1;
+       }
+       if (!strcmp(argv[1], "pcpu")) {
+               print_pcpu_chains();
+       } else {
+               printk("Bad option\n");
+               return 1;
+       }
+       return 0;
+}