Skip to content

Commit bab3d63

Browse files
SMP AP bootup
1 parent a6d41c0 commit bab3d63

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

include/acpi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,5 @@ const char *polarity_str(uint8_t pol);
207207
* @return Descriptive string of the IRQ sharing status
208208
*/
209209
const char *sharing_str(uint8_t share);
210+
211+
void kmain_ap(struct limine_smp_info *info);

src/acpi.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ volatile struct limine_rsdp_request rsdp_request = {
99
.revision = 0,
1010
};
1111

12+
extern volatile struct limine_smp_request smp_request;
13+
1214
static uint8_t lapic_ids[256] = {0}; // CPU core Local APIC IDs
1315
static uint8_t ioapic_ids[256] = {0}; // CPU core Local APIC IDs
1416
static uint16_t numcore = 0; // number of cores detected
@@ -194,6 +196,18 @@ void init_cores() {
194196
}
195197
if (numcore > 0) {
196198
kprintf("SMP: %d cores, %d IOAPICs\n", numcore, numioapic);
199+
if (!smp_request.response) {
200+
kprintf("No SMP response, running uniprocessor.\n");
201+
return;
202+
}
203+
204+
for (uint64_t i = 0; i < smp_request.response->cpu_count; i++) {
205+
struct limine_smp_info *cpu = smp_request.response->cpus[i];
206+
if (cpu->lapic_id == smp_request.response->bsp_lapic_id) {
207+
continue; // Skip BSP
208+
}
209+
cpu->goto_address = kmain_ap;
210+
}
197211
}
198212
for (int i = 0; i < 16; ++i) {
199213
dprintf("IRQ %d maps to GSI %d\n", i, irq_to_gsi(i));

src/ap.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
#include <kernel.h>
22

3-
void kmain_ap()
3+
volatile struct limine_smp_request smp_request = {
4+
.id = LIMINE_SMP_REQUEST,
5+
.revision = 0
6+
};
7+
8+
void kmain_ap(struct limine_smp_info *info)
49
{
10+
kprintf("CPU: %u online; ID: %u\n", info->processor_id, info->lapic_id);
511
wait_forever();
12+
/**
13+
* @todo Insert cpu-local scheduler loop here.
14+
* Each AP will run its own list of executing BASIC processes. Accessing
15+
* the list of other APs and the BSP will be strictly controlled via a
16+
* marshalled lookup system using a spinlock, e.g. if AP 1 wants to check if
17+
* PID X on AP 2 is still running.
18+
*
19+
* This will be done as follows:
20+
*
21+
* 1) Each AP can be instructed via a command queue to launch, query or kill a process.
22+
* 2) Each AP will have its own command queue
23+
* 3) Any AP can push a command onto the command queue for one or more other APs to action
24+
* 4) Initially AP's will wait for a start command in their queue,
25+
* they won't run their scheduler until they receive this command. This allows them all to
26+
* gracefully wait until the first BASIC process is ready to be loaded (/programs/init).
27+
*/
628
}

src/init.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
typedef void (*init_func_t)(void);
44

5+
spinlock_t console_spinlock = 0;
6+
spinlock_t debug_console_spinlock = 0;
7+
58
init_func_t init_funcs[] = {
69
init_heap, validate_limine_page_tables_and_gdt, init_console,
710
init_cores, init_idt, init_pci, init_realtime_clock,
@@ -23,6 +26,8 @@ char* init_funcs_names[] = {
2326

2427
void init()
2528
{
29+
init_spinlock(&debug_console_spinlock);
30+
init_spinlock(&console_spinlock);
2631
uint32_t n = 0;
2732
for (init_func_t* func = init_funcs; *func; ++func) {
2833
dprintf("Bringing up %s...\n", init_funcs_names[n]);

src/printf.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <kernel.h>
22
#include <string.h>
33

4+
extern spinlock_t console_spinlock;
5+
extern spinlock_t debug_console_spinlock;
6+
47
static int do_printf(const char *fmt, size_t max, va_list args, fnptr_t fn, void *ptr)
58
{
69
unsigned flags = 0, actual_wd = 0, count = 0, given_wd = 0;
@@ -276,17 +279,23 @@ int dvprintf_help(unsigned c, [[maybe_unused]] void **ptr, [[maybe_unused]] cons
276279

277280
int vprintf(const char *fmt, va_list args)
278281
{
279-
return do_printf(fmt, SIZE_MAX, args, vprintf_help, NULL);
282+
lock_spinlock(&console_spinlock);
283+
int r = do_printf(fmt, SIZE_MAX, args, vprintf_help, NULL);
284+
unlock_spinlock(&console_spinlock);
285+
return r;
280286
}
281287

282288
int dvprintf(const char *fmt, va_list args)
283289
{
284290
char counter[25];
291+
lock_spinlock(&debug_console_spinlock);
285292
do_itoa(get_ticks(), counter, 10);
286293
dput('[');
287294
dputstring(counter);
288295
dputstring("]: ");
289-
return do_printf(fmt, SIZE_MAX, args, dvprintf_help, NULL);
296+
int r = do_printf(fmt, SIZE_MAX, args, dvprintf_help, NULL);
297+
unlock_spinlock(&debug_console_spinlock);
298+
return r;
290299
}
291300

292301
int printf(const char *fmt, ...)

0 commit comments

Comments
 (0)