Kernel: add asm_helper.h - some asm constructions (outb, inb)

This commit is contained in:
g1n 2021-08-11 14:05:06 +03:00
parent 04b9ba01e8
commit 791679dcd0
2 changed files with 19 additions and 18 deletions

View File

@ -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;
}

View File

@ -4,6 +4,8 @@
#include <limits.h>
#include <stdbool.h>
#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;
}