From 2bdafab229439dcd9968114ab232ed0a3a218bfb Mon Sep 17 00:00:00 2001 From: Tianhao Wang Date: Thu, 1 Feb 2024 04:16:43 +0100 Subject: use inline asm for x86 IO instr --- src/arch/x86_64/asm/io_port.s | 71 ------------------------------------------- src/arch/x86_64/io_port.rs | 44 +++++++++++++++++---------- 2 files changed, 28 insertions(+), 87 deletions(-) delete mode 100644 src/arch/x86_64/asm/io_port.s (limited to 'src/arch') diff --git a/src/arch/x86_64/asm/io_port.s b/src/arch/x86_64/asm/io_port.s deleted file mode 100644 index 3aec69d..0000000 --- a/src/arch/x86_64/asm/io_port.s +++ /dev/null @@ -1,71 +0,0 @@ -;*************************************************************************** -;* Operating-System Construction * -;*---------------------------------------------------------------------------* -;* * -;* I O _ P O R T * -;* * -;*---------------------------------------------------------------------------* -;* The functions defined here encapsulate the machine instructions 'in' and * -;* 'out' for class IO_Port. * -;***************************************************************************** - -; EXPORTED FUNCTIONS - -[GLOBAL _outb] -[GLOBAL _outw] -[GLOBAL _inb] -[GLOBAL _inw] - -; FUNCTION IMPLEMENTATIONS - -[SECTION .text] - -; OUTB: Byte-wise output via an I/O port. -; -; C prototype: void outb (int port, int value); - -_outb: - push rbp - mov rbp, rsp - mov rdx, rdi - mov rax, rsi - out dx, al - pop rbp - ret - -; OUTW: Word-wise output via an I/O port. -; -; C prototype: void outw (int port, int value); - -_outw: - push rbp - mov rbp, rsp - mov rdx, rdi - mov rax, rsi - out dx, ax - pop rbp - ret - -; INB: Byte-wise input via an I/O port. -; -; C prototype: unsigned char inb (int port); - -_inb: - push rbp - mov rbp, rsp - mov rdx, rdi - in al, dx - pop rbp - ret - -; INW: Word-wise input via an I/O port. -; -; C prototype: unsigned short inw (int port); - -_inw: - push rbp - mov rbp, rsp - mov rdx, rdi - in ax, dx - pop rbp - ret diff --git a/src/arch/x86_64/io_port.rs b/src/arch/x86_64/io_port.rs index f5755f4..7b37989 100644 --- a/src/arch/x86_64/io_port.rs +++ b/src/arch/x86_64/io_port.rs @@ -1,29 +1,41 @@ -extern "C" { - fn _inb(port: u16) -> u8; - fn _inw(port: u16) -> u16; - fn _outb(port: u16, val: u8); - fn _outw(port: u16, val: u16); -} - -// The port addr is 16-bit wide. -// wrappers for in/out[b,w] -// Also I don't feel necessary to have a IO_Port Class give how -// trivial it is -// TODO perhaps use inline asm, because the code is short +use core::arch::asm; pub fn inw(p: u16) -> u16 { - unsafe { _inw(p) } + let result: u16; + unsafe { + asm!("in ax, dx", + in("dx") p, + out("ax") result + ) + } + result } pub fn inb(p: u16) -> u8 { - unsafe { _inb(p) } + let result: u8; + unsafe { + asm!("in al, dx", + in("dx") p, + out("al") result + ) + } + result } + pub fn outb(p: u16, val: u8) { unsafe { - _outb(p, val); + asm!("out dx, al", + in("dx") p, + in("al") val, + ) } } pub fn outw(p: u16, val: u16) { - unsafe { _outw(p, val) } + unsafe { + asm!("out dx, ax", + in("dx") p, + in("ax") val, + ) + } } -- cgit v1.2.3-70-g09d2