use new risc-v i/o abi
authorAndrew Waterman <waterman@eecs.berkeley.edu>
Sat, 15 Dec 2012 00:10:00 +0000 (16:10 -0800)
committerAndrew Waterman <waterman@eecs.berkeley.edu>
Sat, 15 Dec 2012 03:36:37 +0000 (19:36 -0800)
kern/arch/riscv/arch.h
kern/arch/riscv/console.c
kern/arch/riscv/console.h
kern/arch/riscv/init.c
kern/arch/riscv/trap.c

index 4ace0df..1479d4f 100644 (file)
@@ -161,9 +161,8 @@ cache_flush(void)
 static __inline void
 reboot(void)
 {
 static __inline void
 reboot(void)
 {
-  extern void fesvr_die();
-       fesvr_die();
-       while(1);
+       extern void cputchar(int ch);
+       cputchar(0);
 }
 
 extern void cpu_halt(void);
 }
 
 extern void cpu_halt(void);
index 184b820..29c7b46 100644 (file)
@@ -7,168 +7,6 @@
 #include <monitor.h>
 #include <process.h>
 
 #include <monitor.h>
 #include <process.h>
 
-struct magic_mem {
-       volatile uint64_t words[8];
-};
-struct fesvr_syscall {
-       struct magic_mem magic_mem;
-       STAILQ_ENTRY(fesvr_syscall) link;
-};
-STAILQ_HEAD(fesvr_syscall_tailq, fesvr_syscall);
-
-spinlock_t fesvr_lock = SPINLOCK_INITIALIZER_IRQSAVE;
-struct fesvr_syscall_tailq fesvr_queue;
-struct magic_mem fesvr_current __attribute__((aligned(64)));
-
-bool fesvr_busy()
-{
-       if (mfpcr(PCR_TOHOST))
-       {
-               assert(core_id() == 0);
-               return true;
-       }
-       
-       volatile uint64_t* mm = fesvr_current.words;
-       if (mfpcr(PCR_FROMHOST) && mm[6])
-       {
-               void (*func)(void*, uint64_t*) = (void*)(uintptr_t)mm[6];
-               void* farg = (void*)(uintptr_t)mm[7];
-               func(farg, (uint64_t*)mm);
-       }
-       mtpcr(PCR_FROMHOST, 0);
-
-       return false;
-}
-
-void fesvr_syscall(long n, long a0, long a1, long a2, long a3,
-                   void (*continuation)(void*, uint64_t*), void* arg)
-{
-       struct fesvr_syscall* mm = kmalloc(sizeof(struct fesvr_syscall), 0);
-       assert(mm);
-
-       mm->magic_mem.words[0] = n;
-       mm->magic_mem.words[1] = a0;
-       mm->magic_mem.words[2] = a1;
-       mm->magic_mem.words[3] = a2;
-       mm->magic_mem.words[4] = a3;
-       mm->magic_mem.words[6] = (uintptr_t)continuation;
-       mm->magic_mem.words[7] = (uintptr_t)arg;
-
-       spin_lock_irqsave(&fesvr_lock);
-       STAILQ_INSERT_TAIL(&fesvr_queue, mm, link);
-       spin_unlock_irqsave(&fesvr_lock);
-}
-
-long fesvr_syscall_sync(long n, long a0, long a1, long a2, long a3)
-{
-       uintptr_t irq_state = disable_irq();
-       while (fesvr_busy());
-
-       struct magic_mem mm __attribute__((aligned(64)));
-       mm.words[0] = n;
-       mm.words[1] = a0;
-       mm.words[2] = a1;
-       mm.words[3] = a2;
-       mm.words[4] = a3;
-
-       mb();
-       mtpcr(PCR_TOHOST, PADDR(&mm));
-       while (mfpcr(PCR_FROMHOST) == 0);
-       mtpcr(PCR_FROMHOST, 0);
-
-       restore_irq(irq_state);
-       return mm.words[0];
-}
-
-void fesvr_die()
-{
-       fesvr_syscall_sync(FESVR_SYS_exit, 0, 0, 0, 0);
-}
-
-// emulate keyboard input with an alarm
-void keyboard_alarm_init()
-{
-       void cons_poll(struct alarm_waiter* awaiter)
-       {
-               static bool cons_polling;
-               static uint64_t cons_last_polled;
-               void cont(void* null, uint64_t* magic_mem)
-               {
-                       for (int i = 0; i < (int)magic_mem[0]; i++)
-                       {
-                               char c = ((char*)KADDR(magic_mem[2]))[i];
-                               if (c == 'G')
-                                       send_kernel_message(core_id(), __run_mon, 0, 0, 0, KMSG_ROUTINE);
-                               else
-                                       send_kernel_message(core_id(), __cons_add_char, (long)&cons_buf,
-                                                           (long)c, 0, KMSG_ROUTINE);
-                       }
-                       cons_polling = false;
-                       cons_last_polled = read_tsc();
-               }
-
-#ifdef __CONFIG_DEMO_SLAVE__
-               if (!fesvr_busy() && STAILQ_EMPTY(&fesvr_queue) && hashtable_count(pid_hash) == 0)
-               {
-                       uint32_t demo_size = 0, demo_pos = 0;
-                       assert(sizeof(demo_size) == fesvr_syscall_sync(FESVR_SYS_read, 0, PADDR(&demo_size), sizeof(demo_size), 0));
-                       void* demo = kmalloc(demo_size, 0);
-                       assert(demo_size == fesvr_syscall_sync(FESVR_SYS_read, 0, PADDR(demo), demo_size, 0));
-                       struct file* f = do_file_open("/bin/demo", O_CREAT, O_WRONLY);
-                       assert(f);
-                       off_t off = 0;
-                       assert(demo_size == f->f_op->write(f, demo, demo_size, &off));
-                       kref_put(&f->f_kref);
-                       /* this is potentially dangerous.  the compiler will put run_demo on
-                        * the stack if it references any other stack variables.  the
-                        * compiler might be allowed to do so otherwise too. */
-                       void run_demo()
-                       {
-                               char *argv[2] = {"", "demo"};
-                               mon_bin_run(2, argv, 0);
-                       }
-                       send_kernel_message(core_id(), run_demo, 0, 0, 0, KMSG_ROUTINE);
-               }
-#else
-               if (!cons_polling && read_tsc() - cons_last_polled >= 100)
-               {
-                       cons_polling = true;
-                       static char buf[64] __attribute__((aligned(64)));
-                       fesvr_syscall(FESVR_SYS_read_noncanonical, 0, PADDR(buf), sizeof(buf), 0, cont, 0);
-               }
-#endif
-
-               uint64_t usec = 100;
-               if (!fesvr_busy())
-               {
-                       spin_lock(&fesvr_lock);
-                       if (!STAILQ_EMPTY(&fesvr_queue))
-                       {
-                               usec = 10;
-                               struct fesvr_syscall* s = STAILQ_FIRST(&fesvr_queue);
-                               fesvr_current = s->magic_mem;
-                               STAILQ_REMOVE_HEAD(&fesvr_queue, link);
-                               kfree(s);
-                               mb();
-                               mtpcr(PCR_TOHOST, PADDR(&fesvr_current));
-                       }
-                       spin_unlock(&fesvr_lock);
-               }
-               else
-                       usec = 10;
-
-               set_awaiter_rel(awaiter, usec);
-               set_alarm(&per_cpu_info[core_id()].tchain, awaiter);
-       }
-
-       STAILQ_INIT(&fesvr_queue);
-
-       static struct alarm_waiter awaiter;
-       init_awaiter(&awaiter, cons_poll);
-       set_awaiter_rel(&awaiter, 1);
-       set_alarm(&per_cpu_info[core_id()].tchain, &awaiter);
-}
-
 int cons_get_any_char(void)
 {
        assert(0);
 int cons_get_any_char(void)
 {
        assert(0);
@@ -177,6 +15,7 @@ int cons_get_any_char(void)
 void
 cons_init(void)
 {
 void
 cons_init(void)
 {
+       while (mtpcr(PCR_TOHOST, 0x0180000000000000));
 }
 
 // `High'-level console I/O.  Used by readline and cprintf.
 }
 
 // `High'-level console I/O.  Used by readline and cprintf.
@@ -184,22 +23,24 @@ cons_init(void)
 void
 cputbuf(const char* str, int len)
 {
 void
 cputbuf(const char* str, int len)
 {
-       extern int booting;
-       if (booting)
-       {
-               fesvr_syscall_sync(FESVR_SYS_write, 1, PADDR(str), len, 0);
-               return;
-       }
-
-       void cont(void* buf, uint64_t* mm)
-       {
-               kfree(buf);
-       }
+       for (int i = 0; i < len; i++)
+               cputchar(str[i]);
+}
 
 
-       char* buf = kmalloc(len, 0);
-       assert(buf);
-       memcpy(buf, str, len);
-       fesvr_syscall(FESVR_SYS_write, 1, PADDR(buf), len, 0, cont, buf);
+void poll_keyboard()
+{
+       uintptr_t fh = mtpcr(PCR_FROMHOST, 0);
+       if (fh == 0)
+               return;
+       assert((fh >> 56) == 0x01);
+
+       char c = fh;
+       if (c == 'G')
+               send_kernel_message(core_id(), __run_mon, 0, 0, 0, KMSG_ROUTINE);
+       else
+               send_kernel_message(core_id(), __cons_add_char, (long)&cons_buf,
+                                   (long)c, 0, KMSG_ROUTINE);
+       cons_init();
 }
 
 // Low-level console I/O
 }
 
 // Low-level console I/O
@@ -207,8 +48,7 @@ cputbuf(const char* str, int len)
 void
 cputchar(int c)
 {
 void
 cputchar(int c)
 {
-       char ch = c;
-       cputbuf(&ch,1);
+       while (mtpcr(PCR_TOHOST, 0x0101000000000000 | (unsigned char)c));
 }
 
 int
 }
 
 int
index 5bd8737..a9e8683 100644 (file)
 #define CRT_SIZE       (CRT_ROWS * CRT_COLS)
 
 void cons_init(void);
 #define CRT_SIZE       (CRT_ROWS * CRT_COLS)
 
 void cons_init(void);
-void keyboard_alarm_init();
 /* Returns any available character, or 0 for none (legacy helper) */
 int cons_get_any_char(void);
 /* Returns any available character, or 0 for none (legacy helper) */
 int cons_get_any_char(void);
-
-#define        FESVR_SYS_exit  1
-#define        FESVR_SYS_getpid        20
-#define        FESVR_SYS_kill  37
-#define        FESVR_SYS_read  3
-#define        FESVR_SYS_write 4
-#define        FESVR_SYS_open  5
-#define        FESVR_SYS_close 6
-#define        FESVR_SYS_lseek 19
-#define        FESVR_SYS_brk           17
-#define        FESVR_SYS_link  9
-#define        FESVR_SYS_unlink        10
-#define        FESVR_SYS_chdir 12
-#define FESVR_SYS_stat 18
-#define FESVR_SYS_fstat        28
-#define        FESVR_SYS_lstat 84
-#define        FESVR_SYS_pread 180
-#define        FESVR_SYS_pwrite 181
-#define FESVR_SYS_getmainvars 201
-#define FESVR_SYS_read_noncanonical 202
-
-void fesvr_die();
+void poll_keyboard();
 
 #endif /* _CONSOLE_H_ */
 
 #endif /* _CONSOLE_H_ */
index d664167..d810da7 100644 (file)
@@ -8,5 +8,4 @@ void arch_init()
 {              
        smp_boot();
        proc_init();
 {              
        smp_boot();
        proc_init();
-       keyboard_alarm_init();
 }
 }
index 59dfcdb..0ac96ab 100644 (file)
@@ -1,6 +1,7 @@
 #include <arch/arch.h>
 #include <assert.h>
 #include <arch/trap.h>
 #include <arch/arch.h>
 #include <assert.h>
 #include <arch/trap.h>
+#include <arch/console.h>
 #include <string.h>
 #include <process.h>
 #include <syscall.h>
 #include <string.h>
 #include <process.h>
 #include <syscall.h>
@@ -121,13 +122,13 @@ static void exit_halt_loop(trapframe_t* tf)
 static void
 handle_ipi(trapframe_t* tf)
 {
 static void
 handle_ipi(trapframe_t* tf)
 {
+       clear_ipi();
+       poll_keyboard(); // keypresses can trigger IPIs
 
        if (!in_kernel(tf))
                set_current_tf(&per_cpu_info[core_id()], tf);
        else
                exit_halt_loop(tf);
 
        if (!in_kernel(tf))
                set_current_tf(&per_cpu_info[core_id()], tf);
        else
                exit_halt_loop(tf);
-       
-       clear_ipi();
 
        handle_kmsg_ipi(tf, 0);
 }
 
        handle_kmsg_ipi(tf, 0);
 }