===================================================================
@@ -114,7 +114,7 @@ void on_cpu_async(int cpu, void (*functi
}
-void smp_init(void)
+void smp_init_ids(void)
{
int i;
void ipi_entry(void);
@@ -125,4 +125,57 @@ void smp_init(void)
for (i = 1; i < cpu_count(); ++i)
on_cpu(i, setup_smp_id, 0);
+ printf("detected %d cpus\n", cpu_count());
+}
+
+static void *smp_function(void)
+{
+ void *fn;
+
+ asm ("mov %%gs:8, %0" : "=r"(fn));
+ return fn;
+}
+
+static void setup_smp_function(void *data)
+{
+ asm ("mov %0, %%gs:8" : : "r"(data) : "memory");
+}
+
+static void *smp_data(void)
+{
+ void *fn;
+
+ asm ("mov %%gs:16, %0" : "=r"(fn));
+ return fn;
+}
+
+static void setup_smp_data(void *data)
+{
+ asm ("mov %0, %%gs:16" : : "r"(data) : "memory");
+}
+
+void on_cpu_noipi(int cpu, void (*function)(void *data), void *data)
+{
+ if (cpu == smp_id())
+ function(data);
+ else {
+ on_cpu(cpu, setup_smp_data, data);
+ on_cpu(cpu, setup_smp_function, function);
+ }
+}
+
+void smp_loop(void)
+{
+ void (*fn)(void *data);
+ void *data;
+
+ asm volatile ("hlt");
+ if (smp_id() == 0)
+ return;
+
+ fn = smp_function();
+ if (fn) {
+ fn(smp_data());
+ setup_smp_function(0);
+ }
}
===================================================================
@@ -5,12 +5,11 @@ struct spinlock {
int v;
};
-void smp_init(void);
-
int cpu_count(void);
int smp_id(void);
void on_cpu(int cpu, void (*function)(void *data), void *data);
void on_cpu_async(int cpu, void (*function)(void *data), void *data);
+void on_cpu_noipi(int cpu, void (*function)(void *data), void *data);
void spin_lock(struct spinlock *lock);
void spin_unlock(struct spinlock *lock);
===================================================================
@@ -165,7 +165,7 @@ ap_start64:
nop
lock incw cpu_online_count
-1: hlt
+1: call smp_loop
jmp 1b
start64:
@@ -174,6 +174,7 @@ start64:
call enable_apic
call smp_init
call enable_x2apic
+ call smp_init_ids
call main
mov %eax, %edi
call exit
===================================================================
@@ -15,8 +15,6 @@ int main()
int ncpus;
int i;
- smp_init();
-
ncpus = cpu_count();
printf("found %d cpus\n", ncpus);
for (i = 0; i < ncpus; ++i)
===================================================================
@@ -155,8 +155,6 @@ int main(void)
{
int i;
- smp_init();
-
for (i = 0; i < ARRAY_SIZE(tests); ++i)
do_test(&tests[i]);