logging: make term and printf smp safe

This commit is contained in:
Matheus
2023-10-01 19:09:28 -03:00
parent 1bb152b446
commit ba16aa366f
7 changed files with 39 additions and 2 deletions
+3
View File
@@ -30,11 +30,14 @@
#include <kernel/block.h>
#include <kernel/ext2.h>
#include <kernel/cmdline.h>
#include <arch/smp.h>
#include <kernel/e1000.h>
static cpu_t bsp_cpu;
void kernel_entry() {
cpu_set(&bsp_cpu);
logging_init();
logging_sethook(arch_e9_putc);
arch_gdt_reload();
arch_idt_setup();
+18
View File
@@ -3,9 +3,27 @@
#include <printf.h>
#include <panic.h>
#include <mutex.h>
#include <arch/cpu.h>
void _putchar(char c);
void logging_sethook(void (*fun)(char));
void logging_init();
extern mutex_t printf_mutex;
#ifdef printf
#undef printf
#endif
#define printf(...) { \
if (_cpu()->thread) {\
MUTEX_ACQUIRE(&printf_mutex, false); \
} else { \
while (MUTEX_TRY(&printf_mutex) == false) CPU_PAUSE(); \
} \
printf_(__VA_ARGS__); \
MUTEX_RELEASE(&printf_mutex); \
}
#define __assert(x) \
if (!(x)){ \
+1
View File
@@ -1,6 +1,7 @@
#ifndef _SEMAPHORE_H
#define _SEMAPHORE_H
#include <stddef.h>
#include <stdbool.h>
#include <spinlock.h>
+1
View File
@@ -34,6 +34,7 @@ typedef struct cpu_t {
} cpu_t;
#define CPU_HALT() asm volatile("hlt")
#define CPU_PAUSE() asm volatile("pause")
static inline cpu_t *_cpu() {
return (cpu_t *)rdmsr(MSR_GSBASE);
+8
View File
@@ -1,3 +1,7 @@
#include <mutex.h>
mutex_t printf_mutex;
static void nohook(char c) {
}
@@ -12,3 +16,7 @@ void _putchar(char c) {
void logging_sethook(void (*fun)(char)) {
hook = fun;
}
void logging_init() {
MUTEX_INIT(&printf_mutex);
}
+5
View File
@@ -20,9 +20,12 @@ static void *internalalloc(size_t n) {
}
static struct flanterm_context *term_ctx;
static mutex_t term_mutex;
void term_write(char *str, size_t count) {
MUTEX_ACQUIRE(&term_mutex, false);
flanterm_write(term_ctx, str, count);
MUTEX_RELEASE(&term_mutex);
}
void term_putchar(char c) {
@@ -55,4 +58,6 @@ void term_init() {
ys = term_ctx->rows;
fbxs = fb->width;
fbys = fb->height;
MUTEX_INIT(&term_mutex);
}
+3 -2
View File
@@ -339,10 +339,11 @@ bool vmm_pagefault(void *addr, bool user, int actions) {
} else {
// anonymous memory. map the zero'd page
status = arch_mmu_map(_cpu()->vmmctx->pagetable, zeropage, addr, range->mmuflags & ~ARCH_MMU_FLAGS_WRITE);
if (!status)
if (!status) {
printf("vmm: out of memory to map zero page into address space\n");
else
} else {
pmm_hold(zeropage);
}
}
} else if (arch_mmu_iswritable(_cpu()->vmmctx->pagetable, addr) == false){
// page present but not writeable in the page tables