From: acevest Date: Sun, 1 Sep 2024 06:01:26 +0000 (+0800) Subject: add ENTER_CRITICAL_ZONE and ENTER_CRITICAL_ZONE X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=9c40b24b39d949dc2ac8ffb71b3f20b7c0500d1c;p=kernel.git add ENTER_CRITICAL_ZONE and ENTER_CRITICAL_ZONE --- diff --git a/include/irq.h b/include/irq.h index bec6002..b0002ba 100644 --- a/include/irq.h +++ b/include/irq.h @@ -76,6 +76,18 @@ bool irq_disabled(); __asm__ __volatile__("pushl %0; popfl" ::"g"(x) : "memory", "cc"); \ } while (0) +void enter_critical_zone(); +void exit_critical_zone(); + +#define ENTER_CRITICAL_ZONE \ + do { \ + enter_critical_zone(); \ + } while (0) +#define EXIT_CRITICAL_ZONE \ + do { \ + exit_critical_zone(); \ + } while (0) + #define IRQ_CLOCK 0x00 #define IRQ_KEYBOARD 0x01 #define IRQ_CASCADE 0x02 diff --git a/kernel/irq.c b/kernel/irq.c index 770f458..83cfada 100644 --- a/kernel/irq.c +++ b/kernel/irq.c @@ -336,3 +336,8 @@ int disable_no_irq_chip(unsigned int irq) { return 0; } irq_chip_t no_irq_chip = {.name = "none", .enable = enable_no_irq_chip, .disable = disable_no_irq_chip}; irq_desc_t no_irq_desc = {.chip = &no_irq_chip, .action = NULL, .status = 0, .depth = 0}; + +// 单CPU +static volatile uint32_t __critical_zone_eflags; +void enter_critical_zone() { irq_save(__critical_zone_eflags); } +void exit_critical_zone() { irq_restore(__critical_zone_eflags); } diff --git a/kernel/printk.c b/kernel/printk.c index 6a0904d..74fd9a1 100644 --- a/kernel/printk.c +++ b/kernel/printk.c @@ -25,37 +25,40 @@ void serial_write(const char *buf, size_t size); char pkbuf[1024]; extern tty_t *const default_tty; int printk(const char *fmtstr, ...) { - unsigned long iflags; - irq_save(iflags); + ENTER_CRITICAL_ZONE; + char *args = (char *)(((char *)&fmtstr) + 4); int size = vsprintf(pkbuf, fmtstr, args); tty_write(default_tty, pkbuf, (size_t)size); serial_write(pkbuf, (size_t)size); - irq_restore(iflags); + + EXIT_CRITICAL_ZONE; return 0; } extern tty_t *const debug_tty; char pdbuf[1024]; int printd(const char *fmtstr, ...) { - unsigned long iflags; - irq_save(iflags); + ENTER_CRITICAL_ZONE; + char *args = (char *)(((char *)&fmtstr) + 4); int size = vsprintf(pdbuf, fmtstr, args); tty_write(debug_tty, pdbuf, (size_t)size); serial_write(pdbuf, (size_t)size); - irq_restore(iflags); + + EXIT_CRITICAL_ZONE; return 0; } char plobuf[1024]; extern tty_t *const monitor_tty; int printlo(unsigned int xpos, unsigned int ypos, const char *fmtstr, ...) { - unsigned long iflags; - irq_save(iflags); + ENTER_CRITICAL_ZONE; + char *args = (char *)(((char *)&fmtstr) + 4); int size = vsprintf(plobuf, fmtstr, args); tty_write_at(monitor_tty, xpos, ypos, plobuf, (size_t)size); - irq_restore(iflags); + + EXIT_CRITICAL_ZONE; return 0; }