vmap: Use {map,unmap}_segment() helpers
[akaros.git] / kern / arch / riscv / smp.c
index bce487f..d064c64 100644 (file)
@@ -8,34 +8,23 @@
 #include <atomic.h>
 #include <pmap.h>
 
-volatile uint32_t num_cpus;
+volatile uint32_t num_cores_booted = 0;
 
 void
 smp_boot(void)
 {
        smp_percpu_init();
-
-       num_cpus = 1;
-       printd("Cores, report in!\n");
-
-       for(uintptr_t i = 1, ncores = num_cores(); i < ncores; i++)
-               send_ipi(i);
-       
-       while(*(volatile uint32_t*)&num_cpus < num_cores());
-
-       printd("%d cores reporting!\n",num_cpus);
+       num_cores_booted = 1;
+       while(num_cores_booted < num_cores);
+       printd("%d cores reporting!\n", num_cores);
 }
 
 void
 smp_init(void)
 {
-       static spinlock_t report_in_lock = SPINLOCK_INITIALIZER;
-
        smp_percpu_init();
-       spin_lock(&report_in_lock);
-       num_cpus++;
-       spin_unlock(&report_in_lock);
 
+       __sync_fetch_and_add(&num_cores_booted, 1);
        printd("Good morning, Vietnam! (core id = %d)\n",core_id());
 
        smp_idle();
@@ -45,7 +34,7 @@ handler_wrapper_t*
 smp_make_wrapper()
 {
        static handler_wrapper_t
-       wrapper_pool[MAX_NUM_CPUS*8] = {{{0},SPINLOCK_INITIALIZER}};
+       wrapper_pool[MAX_NUM_CORES*8] = {{{0},SPINLOCK_INITIALIZER}};
 
        size_t i;
        for(i = 0; i < sizeof(wrapper_pool)/sizeof(wrapper_pool[0]); i++)
@@ -54,26 +43,25 @@ smp_make_wrapper()
        return NULL;
 }
 
-void
-smp_call_wrapper(trapframe_t* tf, uint32_t src, isr_t handler,
-                 handler_wrapper_t* wrapper,void* data)
+void smp_call_wrapper(uint32_t src, isr_t handler, handler_wrapper_t *wrapper,
+                      void *data)
 {
        if(wrapper)
                wrapper->wait_list[core_id()] = 0;
-       handler(tf,data);
+       handler(0, data);
 }
 
 int smp_call_function_self(isr_t handler, void* data,
                            handler_wrapper_t** wait_wrapper)
 {
-       return smp_call_function_single(core_id(),handler,data,wait_wrapper);
+       return smp_call_function_single(core_id(), handler, data, wait_wrapper);
 }
 
 int smp_call_function_all(isr_t handler, void* data,
                           handler_wrapper_t** wait_wrapper)
 {
        int8_t state = 0;
-       int i;
+       int i, me;
        handler_wrapper_t* wrapper = 0;
        if(wait_wrapper)
        {
@@ -81,25 +69,25 @@ int smp_call_function_all(isr_t handler, void* data,
                if(!wrapper)
                        return -ENOMEM;
 
-               for(i = 0; i < num_cpus; i++)
+               for(i = 0; i < num_cores; i++)
                        wrapper->wait_list[i] = 1;
        }
 
        enable_irqsave(&state);
 
        // send to others
-       for(i = 0; i < num_cpus; i++)
+       for(i = 0, me = core_id(); i < num_cores; i++)
        {
-               if(i == core_id())
+               if(i == me)
                        continue;
 
-               send_kernel_message(i,(amr_t)smp_call_wrapper,
-                                         handler, wrapper, data, KMSG_IMMEDIATE);
+               send_kernel_message(i, (amr_t)smp_call_wrapper, (long)handler,
+                                   (long)wrapper, (long)data, KMSG_IMMEDIATE);
        }
 
        // send to me
-       send_kernel_message(core_id(),(amr_t)smp_call_wrapper,
-                                 handler,wrapper,data, KMSG_IMMEDIATE);
+       send_kernel_message(me, (amr_t)smp_call_wrapper, (long)handler,
+                           (long)wrapper, (long)data, KMSG_IMMEDIATE);
 
        cpu_relax(); // wait to get the interrupt
 
@@ -123,8 +111,8 @@ int smp_call_function_single(uint32_t dest, isr_t handler, void* data,
 
        enable_irqsave(&state);
 
-       send_kernel_message(dest,(amr_t)smp_call_wrapper,
-                                 handler,wrapper,data, KMSG_IMMEDIATE);
+       send_kernel_message(dest, (amr_t)smp_call_wrapper, (long)handler,
+                           (long)wrapper, (long)data, KMSG_IMMEDIATE);
 
        cpu_relax(); // wait to get the interrupt, if it's to this core
 
@@ -136,7 +124,7 @@ int smp_call_function_single(uint32_t dest, isr_t handler, void* data,
 int smp_call_wait(handler_wrapper_t* wrapper)
 {
        int i;
-       for(i = 0; i < num_cpus; i++)
+       for(i = 0; i < num_cores; i++)
                while(wrapper->wait_list[i]);
 
        spin_unlock(&wrapper->lock);
@@ -144,12 +132,15 @@ int smp_call_wait(handler_wrapper_t* wrapper)
 }
 
 /* Perform any initialization needed by per_cpu_info.  Right now, this just
- * inits the amsg list (which sparc will probably also want).  Make sure every
- * core calls this at some point in the smp_boot process. */
+ * inits the amsg list.  Make sure every core calls this at some point in the
+ * smp_boot process. */
 void __arch_pcpu_init(uint32_t coreid)
 {
        // Switch to the real L1 page table, rather than the boot page table which
        // has the [0,KERNSIZE-1] identity mapping.
        extern pte_t l1pt[NPTENTRIES];
        lcr3(PADDR(l1pt));
+
+       register uintptr_t sp asm ("sp");
+       set_stack_top(ROUNDUP(sp, PGSIZE));
 }