Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions api/libBareMetal.asm
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

; Kernel functions
b_input equ 0x0000000000100010 ; Scans keyboard for input. OUT: AL = 0 if no key pressed, otherwise ASCII code
b_output equ 0x0000000000100018 ; Displays a number of characters. IN: RSI = message location, RCX = number of characters
b_output equ 0x0000000000100018 ; Displays a number of characters. IN: RSI = Memory address of message, RCX = number of characters

b_net_tx equ 0x0000000000100020 ; Transmit a packet via a network interface. IN: RSI = Memory location where packet is stored, RCX = Length of packet, RDX = Device
b_net_rx equ 0x0000000000100028 ; Polls the network interface for received packet. IN: RDI = Memory location where packet will be stored, RDX = Device. OUT: RCX = Length of packet
b_net_tx equ 0x0000000000100020 ; Transmit a packet via a network interface. IN: RSI = Memory address of where packet is stored, RCX = Length of packet, RDX = Device
b_net_rx equ 0x0000000000100028 ; Polls the network interface for received packet. IN: RDX = Device. OUT: RDI = Memory address of where packet was stored, RCX = Length of packet

b_nvs_read equ 0x0000000000100030 ; Read data from a non-volatile storage device. IN: RAX = Starting sector, RCX = Number of sectors to read, RDX = Device, RDI = Memory location to store data
b_nvs_write equ 0x0000000000100038 ; Write data to a non-volatile storage device. IN: RAX = Starting sector, RCX = Number of sectors to write, RDX = Device, RSI = Memory location of data to store
b_nvs_read equ 0x0000000000100030 ; Read data from a non-volatile storage device. IN: RAX = Starting sector, RCX = Number of sectors to read, RDX = Device, RDI = Memory address to store data
b_nvs_write equ 0x0000000000100038 ; Write data to a non-volatile storage device. IN: RAX = Starting sector, RCX = Number of sectors to write, RDX = Device, RSI = Memory address of data to store

b_system equ 0x0000000000100040 ; Configure system. IN: RCX = Function, RAX = Variable 1, RDX = Variable 2. OUT: RAX = Result

Expand All @@ -35,7 +35,8 @@ SCREEN_X_GET equ 0x21
SCREEN_Y_GET equ 0x22
SCREEN_PPSL_GET equ 0x23
SCREEN_BPP_GET equ 0x24
MAC_GET equ 0x30
NET_STATUS equ 0x30
NET_CONFIG equ 0x31
BUS_READ equ 0x50
BUS_WRITE equ 0x51
STDOUT_SET equ 0x52
Expand Down
2 changes: 1 addition & 1 deletion api/libBareMetal.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void b_net_tx(void *mem, u64 len, u64 iid) {

u64 b_net_rx(void *mem, u64 iid) {
u64 tlong;
asm volatile ("call *0x00100028" : "=c"(tlong) : "D"(mem), "d"(iid));
asm volatile ("call *0x00100028" : "=D"(mem), "=c"(tlong) : "d"(iid));
return tlong;
}

Expand Down
3 changes: 2 additions & 1 deletion api/libBareMetal.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ u64 b_system(u64 function, u64 var1, u64 var2);
#define SCREEN_Y_GET 0x22
#define SCREEN_PPSL_GET 0x23
#define SCREEN_BPP_GET 0x24
#define MAC_GET 0x30
#define NET_STATUS 0x30
#define NET_CONFIG 0x31
#define BUS_READ 0x50
#define BUS_WRITE 0x51
#define STDOUT_SET 0x52
Expand Down
74 changes: 48 additions & 26 deletions src/drivers/net/i8254x.asm
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ net_i8254x_init:
add rdi, rax

mov ax, 0x8254 ; Driver tag for i8254x
stosw
add rdi, 14
mov [rdi+nt_ID], ax

; Get the Base Memory Address of the device
mov al, 0 ; Read BAR0
call os_bus_read_bar
stosq ; Save the base
mov [rdi+nt_base], rax ; Save the base
push rax ; Save the base for gathering the MAC later
mov rax, rcx
stosq ; Save the length

; Set PCI Status/Command values
mov dl, 0x01 ; Read Status/Command
Expand All @@ -44,7 +41,8 @@ net_i8254x_init:

; Get the MAC address
pop rsi ; Restore the base
sub rdi, 24 ; 8 bytes into net table entry
push rdi
add rdi, 8
mov eax, [rsi+i8254x_RAL] ; RAL
stosb
shr eax, 8
Expand All @@ -57,31 +55,32 @@ net_i8254x_init:
stosb
shr eax, 8
stosb
pop rdi

; Set base addresses for TX and RX descriptors
xor ecx, ecx
mov cl, byte [os_net_icount]
shl ecx, 15

add rdi, 0x22
mov rax, os_tx_desc
add rax, rcx
stosq
mov [rdi+nt_tx_desc], rax
mov rax, os_rx_desc
add rax, rcx
stosq
mov [rdi+nt_rx_desc], rax

; Reset the device
xor edx, edx
mov dl, [os_net_icount]
call net_i8254x_reset

; Store call addresses
sub rdi, 0x20
mov rax, net_i8254x_config
mov [rdi+nt_config], rax
mov rax, net_i8254x_transmit
stosq
mov [rdi+nt_transmit], rax
mov rax, net_i8254x_poll
stosq
mov [rdi+nt_poll], rax

net_i8254x_init_error:

Expand Down Expand Up @@ -215,6 +214,34 @@ net_i8254x_reset_nextdesc:
; -----------------------------------------------------------------------------


; -----------------------------------------------------------------------------
; net_i8254x_config -
; IN: RAX = Base address to store packets
; RDX = Interface ID
; OUT: Nothing
net_i8254x_config:
push rdi
push rcx
push rax

mov rdi, [rdx+nt_rx_desc]
mov ecx, i8254x_MAX_DESC
call os_virt_to_phys
net_i8254x_config_next_record:
stosq
add rdi, 8
add rax, 2048
dec ecx
cmp ecx, 0
jnz net_i8254x_config_next_record

pop rax
pop rcx
pop rdi
ret
; -----------------------------------------------------------------------------


; -----------------------------------------------------------------------------
; net_i8254x_transmit - Transmit a packet via an Intel 8254x NIC
; IN: RSI = Location of packet
Expand Down Expand Up @@ -270,9 +297,9 @@ net_i8254x_transmit:

; -----------------------------------------------------------------------------
; net_i8254x_poll - Polls the Intel 8254x NIC for a received packet
; IN: RDI = Location to store packet
; RDX = Interface ID
; OUT: RCX = Length of packet
; IN: RDX = Interface ID
; OUT: RDI = Location of stored packet
; RCX = Length of packet
; Note: RDESC Descriptor Format:
; First Qword:
; Bits 63:0 - Buffer Address
Expand All @@ -283,8 +310,8 @@ net_i8254x_transmit:
; Bits 47:40 - Errors
; Bits 63:48 - Special
net_i8254x_poll:
push rdi
push rsi ; Used for the base MMIO of the NIC
push rbx
push rax

mov rdi, [rdx+nt_rx_desc]
Expand All @@ -293,8 +320,9 @@ net_i8254x_poll:
; Calculate the descriptor to read from
mov eax, [rdx+nt_rx_head] ; Get rx_lasthead
shl eax, 4 ; Quick multiply by 16
add eax, 8 ; Offset to bytes received
add rdi, rax ; Add offset to RDI
mov rbx, [rdi]
add rdi, 8 ; Offset to bytes received
; Todo: read all 64 bits. check status bit for DD
xor ecx, ecx ; Clear RCX
mov cx, [rdi] ; Get the packet length
Expand All @@ -303,6 +331,7 @@ net_i8254x_poll:

xor eax, eax
stosq ; Clear the descriptor length and status
mov rdi, rbx

; Increment i8254x_rx_lasthead and the Receive Descriptor Tail
mov eax, [rdx+nt_rx_head] ; Get rx_lasthead
Expand All @@ -315,16 +344,10 @@ net_i8254x_poll:
and eax, i8254x_MAX_DESC - 1
mov [rsi+i8254x_RDT], eax ; Write the updated Receive Descriptor Tail

pop rax
pop rsi
pop rdi
ret

net_i8254x_poll_end:
xor ecx, ecx
pop rax
pop rbx
pop rsi
pop rdi
ret
; -----------------------------------------------------------------------------

Expand All @@ -346,8 +369,7 @@ net_i8254x_poll_end:


; Constants
i8254x_MAX_PKT_SIZE equ 16384
i8254x_MAX_DESC equ 16 ; Must be 16, 32, 64, 128, etc.
i8254x_MAX_DESC equ 2048 ; Must be 16, 32, 64, 128, etc. Each descriptor is 16 bytes

; Register list (13.2) (All registers should be accessed as 32-bit values)

Expand Down
68 changes: 45 additions & 23 deletions src/drivers/net/i8257x.asm
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ net_i8257x_init:
add rdi, rax

mov ax, 0x8257 ; Driver tag for i8257x
stosw
add rdi, 14
mov [rdi+nt_ID], ax

; Get the Base Memory Address of the device
mov al, 0 ; Read BAR0
call os_bus_read_bar
stosq ; Save the base
mov [rdi+nt_base], rax ; Save the base
push rax ; Save the base for gathering the MAC later
mov rax, rcx
stosq ; Save the length

; Set PCI Status/Command values
mov dl, 0x01 ; Read Status/Command
Expand All @@ -47,7 +44,8 @@ net_i8257x_init:

; Get the MAC address
pop rsi ; Restore the base
sub rdi, 24 ; 8 bytes into net table entry
push rdi
add rdi, 8
mov eax, [rsi+i8257x_RAL] ; RAL
stosb
shr eax, 8
Expand All @@ -60,31 +58,32 @@ net_i8257x_init:
stosb
shr eax, 8
stosb
pop rdi

; Set base addresses for TX and RX descriptors
xor ecx, ecx
mov cl, byte [os_net_icount]
shl ecx, 15

add rdi, 0x22
mov rax, os_tx_desc
add rax, rcx
stosq
mov [rdi+nt_tx_desc], rax
mov rax, os_rx_desc
add rax, rcx
stosq
mov [rdi+nt_rx_desc], rax

; Reset the device
xor edx, edx
mov dl, [os_net_icount]
call net_i8257x_reset

; Store call addresses
sub rdi, 0x20
mov rax, net_i8257x_config
mov [rdi+nt_config], rax
mov rax, net_i8257x_transmit
stosq
mov [rdi+nt_transmit], rax
mov rax, net_i8257x_poll
stosq
mov [rdi+nt_poll], rax

net_i8257x_init_error:

Expand Down Expand Up @@ -217,6 +216,34 @@ net_i8257x_reset_nextdesc:
; -----------------------------------------------------------------------------


; -----------------------------------------------------------------------------
; net_i8257x_config -
; IN: RAX = Base address to store packets
; RDX = Interface ID
; OUT: Nothing
net_i8257x_config:
push rdi
push rcx
push rax

mov rdi, [rdx+nt_rx_desc]
mov ecx, i8257x_MAX_DESC
call os_virt_to_phys
net_i8257x_config_next_record:
stosq
add rdi, 8
add rax, 2048
dec ecx
cmp ecx, 0
jnz net_i8257x_config_next_record

pop rax
pop rcx
pop rdi
ret
; -----------------------------------------------------------------------------


; -----------------------------------------------------------------------------
; net_i8257x_transmit - Transmit a packet via an Intel 8257x NIC
; IN: RSI = Location of packet
Expand Down Expand Up @@ -285,8 +312,8 @@ net_i8257x_transmit:
; Bits 47:40 - Errors
; Bits 63:48 - VLAN
net_i8257x_poll:
push rdi
push rsi ; Used for the base MMIO of the NIC
push rbx
push rax

mov rdi, [rdx+nt_rx_desc]
Expand All @@ -295,8 +322,9 @@ net_i8257x_poll:
; Calculate the descriptor to read from
mov eax, [rdx+nt_rx_head] ; Get rx_lasthead
shl eax, 4 ; Quick multiply by 16
add eax, 8 ; Offset to bytes received
add rdi, rax ; Add offset to RDI
mov rbx, [rdi]
add rdi, 8 ; Offset to bytes received
; Todo: read all 64 bits. check status bit for DD
xor ecx, ecx ; Clear RCX
mov cx, [rdi] ; Get the packet length
Expand All @@ -305,6 +333,7 @@ net_i8257x_poll:

xor eax, eax
stosq ; Clear the descriptor length and status
mov rdi, rbx

; Increment i8257x_rx_lasthead and the Receive Descriptor Tail
mov eax, [rdx+nt_rx_head] ; Get rx_lasthead
Expand All @@ -317,23 +346,16 @@ net_i8257x_poll:
and eax, i8257x_MAX_DESC - 1
mov [rsi+i8257x_RDT], eax ; Write the updated Receive Descriptor Tail

pop rax
pop rsi
pop rdi
ret

net_i8257x_poll_end:
xor ecx, ecx
pop rax
pop rbx
pop rsi
pop rdi
ret
; -----------------------------------------------------------------------------


; Constants
i8257x_MAX_PKT_SIZE equ 16384
i8257x_MAX_DESC equ 16 ; Must be 16, 32, 64, 128, etc.
i8257x_MAX_DESC equ 2048 ; Must be 16, 32, 64, 128, etc. Each descriptor is 16 bytes

; Register list (13.3) (All registers should be accessed as 32-bit values)

Expand Down
Loading