Skip to content

Commit f2306f9

Browse files
fix printf formats, introduce simple_cv to spin and wait for a start signal
1 parent f47164a commit f2306f9

26 files changed

+129
-76
lines changed

include/cv.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include <kernel.h>
3+
#include <stdatomic.h>
4+
5+
typedef struct simple_cv {
6+
atomic_int waiting;
7+
} simple_cv_t;
8+
9+
static inline void simple_cv_init(simple_cv_t *cv) {
10+
atomic_store(&cv->waiting, 0);
11+
}
12+
13+
// called by consumer
14+
static inline void simple_cv_wait(simple_cv_t *cv) {
15+
int id = atomic_fetch_add(&cv->waiting, 1) + 1;
16+
17+
// spin until producer broadcasts
18+
while (atomic_load(&cv->waiting) >= id) {
19+
__builtin_ia32_pause();
20+
}
21+
}
22+
23+
// called by producer
24+
static inline void simple_cv_broadcast(simple_cv_t *cv) {
25+
atomic_store(&cv->waiting, 0);
26+
}

include/kernel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
*/
66
#pragma once
77

8+
#define kprintf printf
89
#include <stdint.h>
910
#include <stdbool.h>
1011
#include <stddef.h>
1112
#include <limits.h>
12-
#define kprintf printf
1313
#include <limine.h>
1414
#include "idt.h"
1515
#include "spinlock.h"
1616
#include "rwlock.h"
17+
#include "cv.h"
1718
#include "printf.h"
1819
#include "hashmap.h"
1920
#include "random.h"

src/acpi.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void init_uacpi(void) {
5959
uint64_t end_tsc = rdtsc();
6060
tsc_per_sec = end_tsc - start_tsc;
6161
mhz = tsc_per_sec / 1000000;
62-
dprintf("mhz = %llu, tsc_per_sec = %llu\n", mhz, tsc_per_sec);
62+
dprintf("mhz = %lu, tsc_per_sec = %lu\n", mhz, tsc_per_sec);
6363

6464
dprintf("init_uacpi uacpi_initialize(0)\n");
6565
uacpi_context_set_log_level(UACPI_LOG_INFO);
@@ -148,7 +148,7 @@ void init_acpi() {
148148
if (*ptr == 'A' && *(ptr + 1) == 'P' && *(ptr + 2) == 'I' && *(ptr + 3) == 'C') {
149149
// found MADT
150150
lapic_ptr = (uint64_t) (*((uint32_t *) (ptr + 0x24)));
151-
kprintf("Detected: 32-Bit Local APIC [base: %llx]\n", lapic_ptr);
151+
kprintf("Detected: 32-Bit Local APIC [base: %lx]\n", lapic_ptr);
152152
ptr2 = ptr + *((uint32_t *) (ptr + 4));
153153
// iterate on variable length records
154154
for (ptr += 44; ptr < ptr2; ptr += ptr[1]) {
@@ -166,7 +166,7 @@ void init_acpi() {
166166
mmio[0] = 0x01;
167167
uint32_t count = ((mmio[0x10 / 4]) & 0xFF) + 1;
168168
ioapic_gsi_count[numioapic] = count;
169-
kprintf("Detected: IOAPIC [base: %llx; id: %d gsi base: %d gsi count: %d]\n",
169+
kprintf("Detected: IOAPIC [base: %lx; id: %d gsi base: %d gsi count: %d]\n",
170170
ioapic_ptr[numioapic], ioapic_ids[numioapic],
171171
ioapic_gsi_base[numioapic], count);
172172
numioapic++;
@@ -186,7 +186,7 @@ void init_acpi() {
186186
}
187187
case 5:
188188
lapic_ptr = *((uint64_t *) (ptr + 4));
189-
kprintf("Detected: 64-Bit Local APIC [base: %llx]\n", lapic_ptr);
189+
kprintf("Detected: 64-Bit Local APIC [base: %lx]\n", lapic_ptr);
190190
break; // found 64 bit LAPIC
191191
}
192192
}

src/ahci.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ int storage_device_ahci_block_read(void* dev, uint64_t start, uint32_t bytes, un
115115
if (!sd) {
116116
return 0;
117117
}
118-
dprintf("storage_device_ahci_block_read bytes=%d\n", bytes);
119118
uint32_t divided_length = bytes / sd->block_size;
120119
ahci_hba_mem_t* abar = (ahci_hba_mem_t*)sd->opaque2;
121120
ahci_hba_port_t* port = &abar->ports[sd->opaque1];
@@ -129,8 +128,6 @@ int storage_device_ahci_block_read(void* dev, uint64_t start, uint32_t bytes, un
129128

130129
size_t max_per_read = AHCI_DEV_SATAPI ? 1 : 16;
131130

132-
dprintf("divided length: %d\n", divided_length);
133-
134131
if (divided_length < max_per_read) {
135132
return check_type(port) == AHCI_DEV_SATAPI ? ahci_atapi_read(port, start, divided_length, (uint16_t*)buffer, abar) : ahci_read(port, start, divided_length, (uint16_t*)buffer, abar);
136133
}
@@ -296,7 +293,7 @@ bool ahci_read(ahci_hba_port_t *port, uint64_t start, uint32_t count, uint16_t *
296293
spin++;
297294
}
298295
if (spin == 1000000) {
299-
dprintf("Port hung (start %llx count %llx)\n", start, count);
296+
dprintf("Port hung (start %lx count %x)\n", start, count);
300297
return false;
301298
}
302299

@@ -309,14 +306,14 @@ bool ahci_read(ahci_hba_port_t *port, uint64_t start, uint32_t count, uint16_t *
309306
if ((port->ci & (1<<slot)) == 0)
310307
break;
311308
if (port->is & HBA_PxIS_TFES) { // Task file error
312-
dprintf("Read disk error (start %llx count %llx)\n", start, count);
309+
dprintf("Read disk error (start %lx count %x)\n", start, count);
313310
return false;
314311
}
315312
}
316313

317314
// Check again
318315
if (port->is & HBA_PxIS_TFES) {
319-
dprintf("Read disk error (start %llx count %llx)\n", start, count);
316+
dprintf("Read disk error (start %lx count %x)\n", start, count);
320317
return false;
321318
}
322319

@@ -414,8 +411,6 @@ bool ahci_atapi_read(ahci_hba_port_t *port, uint64_t start, uint32_t count, uint
414411
return false;
415412
}
416413

417-
dprintf("ahci_atapi_read %d %d %016x\n", start, count, buf);
418-
419414
ahci_hba_cmd_header_t* cmdheader = (ahci_hba_cmd_header_t*)(uint64_t)(port->clb);
420415
cmdheader += slot;
421416

@@ -463,7 +458,7 @@ bool ahci_atapi_read(ahci_hba_port_t *port, uint64_t start, uint32_t count, uint
463458
};
464459

465460
if (spin == 1000000) {
466-
dprintf("Port hung [atapi] (start %llx count %llx)\n", start, count);
461+
dprintf("Port hung [atapi] (start %lx count %x)\n", start, count);
467462
return false;
468463
}
469464

@@ -472,13 +467,13 @@ bool ahci_atapi_read(ahci_hba_port_t *port, uint64_t start, uint32_t count, uint
472467
while(true) {
473468
if ((port->ci & (1<<slot)) == 0) break;
474469
if (port->is & HBA_PxIS_TFES) {
475-
dprintf("Read disk error [atapi] (start %llx count %llx)\n", start, count);
470+
dprintf("Read disk error [atapi] (start %lx count %x)\n", start, count);
476471
return false;
477472
}
478473
}
479474

480475
if (port->is & HBA_PxIS_TFES) {
481-
dprintf("Read disk error [atapi] (start %llx count %llx)\n", start, count);
476+
dprintf("Read disk error [atapi] (start %lx count %x)\n", start, count);
482477
return false;
483478
}
484479

@@ -548,7 +543,7 @@ bool ahci_write(ahci_hba_port_t *port, uint64_t start, uint32_t count, char *buf
548543
spin++;
549544
}
550545
if (spin == 1000000) {
551-
dprintf("Port hung [write] (start %llx count %llx)\n", start, count);
546+
dprintf("Port hung [write] (start %lx count %x)\n", start, count);
552547
return false;
553548
}
554549

@@ -559,14 +554,14 @@ bool ahci_write(ahci_hba_port_t *port, uint64_t start, uint32_t count, char *buf
559554
if ((port->ci & (1<<slot)) == 0)
560555
break;
561556
if (port->is & HBA_PxIS_TFES) { // Task file error
562-
dprintf("Write disk error (start %llx count %llx)\n", start, count);
557+
dprintf("Write disk error (start %lx count %x)\n", start, count);
563558
return false;
564559
}
565560
}
566561

567562
// Check again
568563
if (port->is & HBA_PxIS_TFES) {
569-
dprintf("Write disk error (start %llx count %llx)\n", start, count);
564+
dprintf("Write disk error (start %lx count %x)\n", start, count);
570565
return false;
571566
}
572567

@@ -595,7 +590,7 @@ void init_ahci()
595590

596591
ahci_base = pci_mem_base(ahci_base);
597592
uint32_t irq_num = pci_setup_interrupt("AHCI", ahci_device, logical_cpu_id(), ahci_handler, ahci_base);
598-
dprintf("AHCI base MMIO: %08x INT %d\n", ahci_base, irq_num);
593+
dprintf("AHCI base MMIO: %lx INT %d\n", ahci_base, irq_num);
599594

600595
probe_port((ahci_hba_mem_t*)ahci_base, ahci_device);
601596
}

src/ap.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ volatile struct limine_smp_request smp_request = {
99
extern volatile idt_ptr_t idt64;
1010

1111
atomic_size_t aps_online = 0;
12+
simple_cv_t boot_condition;
1213

1314
void kmain_ap(struct limine_smp_info *info)
1415
{
@@ -19,9 +20,7 @@ void kmain_ap(struct limine_smp_info *info)
1920
kprintf("CPU: %u online; ID: %u\n", info->processor_id, info->lapic_id);
2021
atomic_fetch_add(&aps_online, 1);
2122

22-
for(;;) {
23-
_mm_pause();
24-
}
23+
simple_cv_wait(&boot_condition);
2524
/**
2625
* @todo Insert cpu-local scheduler loop here.
2726
* Each AP will run its own list of executing BASIC processes. Accessing
@@ -41,4 +40,6 @@ void kmain_ap(struct limine_smp_info *info)
4140
* state, so we dont have to peek at another scheduler instance's command queue to
4241
* see if a process ours is waiting on still lives.
4342
*/
43+
dprintf("Got start signal on cpu #%d\n", info->processor_id);
44+
wait_forever();
4445
}

src/basic/array.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const char* basic_get_string_array_variable(const char* var, int64_t index, stru
6161
return "";
6262
}
6363
}
64-
dprintf("Invalid string array variable '%s' index '%d'\n", var, index);
64+
dprintf("Invalid string array variable '%s' index '%ld'\n", var, index);
6565
tokenizer_error_print(ctx, "No such array variable");
6666
return "";
6767
}
@@ -82,7 +82,7 @@ int64_t basic_get_int_array_variable(const char* var, int64_t index, struct basi
8282
return cur->values[index];
8383
}
8484
}
85-
dprintf("Invalid int array variable '%s' index '%d'\n", var, index);
85+
dprintf("Invalid int array variable '%s' index '%ld'\n", var, index);
8686
tokenizer_error_print(ctx, "No such array variable");
8787
return 0;
8888
}
@@ -105,7 +105,7 @@ bool basic_get_double_array_variable(const char* var, int64_t index, struct basi
105105
return true;
106106
}
107107
}
108-
dprintf("Invalid float array variable '%s' index '%d'\n", var, index);
108+
dprintf("Invalid float array variable '%s' index '%ld'\n", var, index);
109109
tokenizer_error_print(ctx, "No such array variable");
110110
*ret = 0;
111111
return false;

src/basic/console.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*/
55
#include <kernel.h>
66

7+
extern spinlock_t console_spinlock;
8+
extern spinlock_t debug_console_spinlock;
9+
710
int64_t basic_get_text_max_x(struct basic_ctx* ctx)
811
{
912
return get_text_width();
@@ -122,7 +125,11 @@ void print_statement(struct basic_ctx* ctx)
122125
accept_or_return(PRINT, ctx);
123126
const char* out = printable_syntax(ctx);
124127
if (out) {
128+
lock_spinlock(&console_spinlock);
129+
lock_spinlock(&debug_console_spinlock);
125130
putstring((console*)ctx->cons, out);
131+
unlock_spinlock(&console_spinlock);
132+
unlock_spinlock(&debug_console_spinlock);
126133
}
127134
}
128135

src/basic/function.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ int64_t basic_eval_int_fn(const char* fn_name, struct basic_ctx* ctx)
283283
while (extract_comma_list(def, ctx));
284284
struct basic_ctx* atomic = basic_clone(ctx);
285285
atomic->fn_type = RT_INT;
286-
dprintf("Function eval, jump to line %d\n", def->line);
286+
dprintf("Function eval, jump to line %ld\n", def->line);
287287
jump_linenum(def->line, atomic);
288288

289289
while (!basic_finished(atomic)) {

src/basic/string.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ char* basic_str(struct basic_ctx* ctx)
1919
PARAMS_GET_ITEM(BIP_INT);
2020
PARAMS_END("STR$","");
2121
char res[MAX_STRINGLEN];
22-
snprintf(res, MAX_STRINGLEN, "%lld", intval);
22+
snprintf(res, MAX_STRINGLEN, "%ld", intval);
2323
return gc_strdup(res);
2424
}
2525

src/basic/tokenizer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ void tokenizer_error_print(struct basic_ctx* ctx, const char* error)
333333
set_video_auto_flip(true);
334334
}
335335
setforeground(current_console, COLOUR_LIGHTRED);
336-
kprintf("Error on line %d: %s\n", ctx->current_linenum, error);
336+
kprintf("Error on line %ld: %s\n", ctx->current_linenum, error);
337337
setforeground(current_console, COLOUR_WHITE);
338338
ctx->ended = true;
339339
}

0 commit comments

Comments
 (0)