From 791679dcd0a2bd8880c81bdd3fe3ffa8c0527de5 Mon Sep 17 00:00:00 2001 From: g1n Date: Wed, 11 Aug 2021 14:05:06 +0300 Subject: [PATCH] Kernel: add asm_helper.h - some asm constructions (outb, inb) --- kernel/kernel/asm_helper.h | 17 +++++++++++++++++ kernel/kernel/serial.h | 20 ++------------------ 2 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 kernel/kernel/asm_helper.h diff --git a/kernel/kernel/asm_helper.h b/kernel/kernel/asm_helper.h new file mode 100644 index 0000000..60c8a1d --- /dev/null +++ b/kernel/kernel/asm_helper.h @@ -0,0 +1,17 @@ +inline void outb(uint16_t port, uint8_t val) +{ + asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) ); + /* There's an outb %al, $imm8 encoding, for compile-time constant port numbers that fit in 8b. (N constraint). + * Wider immediate constants would be truncated at assemble-time (e.g. "i" constraint). + * The outb %al, %dx encoding is the only option for all other cases. + * %1 expands to %dx because port is a uint16_t. %w1 could be used if we had the port number a wider C type */ +} + +inline int inb(int port) +{ + uint8_t ret; + asm volatile ( "inb %1, %0" + : "=a"(ret) + : "Nd"(port) ); + return ret; +} diff --git a/kernel/kernel/serial.h b/kernel/kernel/serial.h index 94eb14d..abb11ca 100644 --- a/kernel/kernel/serial.h +++ b/kernel/kernel/serial.h @@ -4,6 +4,8 @@ #include #include +#include "asm_helper.h" + #define PORT 0x3f8 // COM1 static int init_serial() { @@ -127,21 +129,3 @@ int serial_printf(const char* restrict format, ...) { va_end(parameters); return written; } - -inline void outb(uint16_t port, uint8_t val) -{ - asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) ); - /* There's an outb %al, $imm8 encoding, for compile-time constant port numbers that fit in 8b. (N constraint). - * Wider immediate constants would be truncated at assemble-time (e.g. "i" constraint). - * The outb %al, %dx encoding is the only option for all other cases. - * %1 expands to %dx because port is a uint16_t. %w1 could be used if we had the port number a wider C type */ -} - -inline int inb(int port) -{ - uint8_t ret; - asm volatile ( "inb %1, %0" - : "=a"(ret) - : "Nd"(port) ); - return ret; -}