mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-11-01 06:58:22 +00:00
logging: make term and printf smp safe
This commit is contained in:
@@ -30,11 +30,14 @@
|
|||||||
#include <kernel/block.h>
|
#include <kernel/block.h>
|
||||||
#include <kernel/ext2.h>
|
#include <kernel/ext2.h>
|
||||||
#include <kernel/cmdline.h>
|
#include <kernel/cmdline.h>
|
||||||
|
#include <arch/smp.h>
|
||||||
|
#include <kernel/e1000.h>
|
||||||
|
|
||||||
static cpu_t bsp_cpu;
|
static cpu_t bsp_cpu;
|
||||||
|
|
||||||
void kernel_entry() {
|
void kernel_entry() {
|
||||||
cpu_set(&bsp_cpu);
|
cpu_set(&bsp_cpu);
|
||||||
|
logging_init();
|
||||||
logging_sethook(arch_e9_putc);
|
logging_sethook(arch_e9_putc);
|
||||||
arch_gdt_reload();
|
arch_gdt_reload();
|
||||||
arch_idt_setup();
|
arch_idt_setup();
|
||||||
|
|||||||
@@ -3,9 +3,27 @@
|
|||||||
|
|
||||||
#include <printf.h>
|
#include <printf.h>
|
||||||
#include <panic.h>
|
#include <panic.h>
|
||||||
|
#include <mutex.h>
|
||||||
|
#include <arch/cpu.h>
|
||||||
|
|
||||||
void _putchar(char c);
|
void _putchar(char c);
|
||||||
void logging_sethook(void (*fun)(char));
|
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) \
|
#define __assert(x) \
|
||||||
if (!(x)){ \
|
if (!(x)){ \
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef _SEMAPHORE_H
|
#ifndef _SEMAPHORE_H
|
||||||
#define _SEMAPHORE_H
|
#define _SEMAPHORE_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <spinlock.h>
|
#include <spinlock.h>
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ typedef struct cpu_t {
|
|||||||
} cpu_t;
|
} cpu_t;
|
||||||
|
|
||||||
#define CPU_HALT() asm volatile("hlt")
|
#define CPU_HALT() asm volatile("hlt")
|
||||||
|
#define CPU_PAUSE() asm volatile("pause")
|
||||||
|
|
||||||
static inline cpu_t *_cpu() {
|
static inline cpu_t *_cpu() {
|
||||||
return (cpu_t *)rdmsr(MSR_GSBASE);
|
return (cpu_t *)rdmsr(MSR_GSBASE);
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
#include <mutex.h>
|
||||||
|
|
||||||
|
mutex_t printf_mutex;
|
||||||
|
|
||||||
static void nohook(char c) {
|
static void nohook(char c) {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,3 +16,7 @@ void _putchar(char c) {
|
|||||||
void logging_sethook(void (*fun)(char)) {
|
void logging_sethook(void (*fun)(char)) {
|
||||||
hook = fun;
|
hook = fun;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void logging_init() {
|
||||||
|
MUTEX_INIT(&printf_mutex);
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,9 +20,12 @@ static void *internalalloc(size_t n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct flanterm_context *term_ctx;
|
static struct flanterm_context *term_ctx;
|
||||||
|
static mutex_t term_mutex;
|
||||||
|
|
||||||
void term_write(char *str, size_t count) {
|
void term_write(char *str, size_t count) {
|
||||||
|
MUTEX_ACQUIRE(&term_mutex, false);
|
||||||
flanterm_write(term_ctx, str, count);
|
flanterm_write(term_ctx, str, count);
|
||||||
|
MUTEX_RELEASE(&term_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void term_putchar(char c) {
|
void term_putchar(char c) {
|
||||||
@@ -55,4 +58,6 @@ void term_init() {
|
|||||||
ys = term_ctx->rows;
|
ys = term_ctx->rows;
|
||||||
fbxs = fb->width;
|
fbxs = fb->width;
|
||||||
fbys = fb->height;
|
fbys = fb->height;
|
||||||
|
|
||||||
|
MUTEX_INIT(&term_mutex);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -339,10 +339,11 @@ bool vmm_pagefault(void *addr, bool user, int actions) {
|
|||||||
} else {
|
} else {
|
||||||
// anonymous memory. map the zero'd page
|
// anonymous memory. map the zero'd page
|
||||||
status = arch_mmu_map(_cpu()->vmmctx->pagetable, zeropage, addr, range->mmuflags & ~ARCH_MMU_FLAGS_WRITE);
|
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");
|
printf("vmm: out of memory to map zero page into address space\n");
|
||||||
else
|
} else {
|
||||||
pmm_hold(zeropage);
|
pmm_hold(zeropage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (arch_mmu_iswritable(_cpu()->vmmctx->pagetable, addr) == false){
|
} else if (arch_mmu_iswritable(_cpu()->vmmctx->pagetable, addr) == false){
|
||||||
// page present but not writeable in the page tables
|
// page present but not writeable in the page tables
|
||||||
|
|||||||
Reference in New Issue
Block a user