Skip to content
Draft
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
2 changes: 1 addition & 1 deletion src/kernel/arch/riscv64/paging.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void mm_paging_fence(void) {
sfence_vma();
}

static bool mm_paging_walk(uaddr va, paddr *pte, bool alloc) {
bool mm_paging_walk(uaddr va, paddr *pte, bool alloc) {
assert(is_aligned(va, 12), "va={uaddr}", va);
assert((uaddr)(((iaddr)va >> 39) + 1) <= 1, "va={uaddr}", va);
assert(pte);
Expand Down
154 changes: 154 additions & 0 deletions src/kernel/drivers/rtl8139.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include <arch/riscv64/insns.h>

#include <drivers/rtl8139.h>

#include <mm/paging.h>
#include <mm/virtual_alloc.h>
#include <physical.h>
#include <print.h>


constexpr usize TX_BUFF_SIZE = 0x1700;

enum RTL8139_REGS {
REG_COM = 0x37,
REG_TCR = 0x40,
REG_CONFIG1 = 0x52
};

enum RTL8139_CONST {
TSD_TOK = (1 << 15),
TSD_OWN = (1 << 13),

COM_RST = (1 << 4),
COM_RE = (1 << 3),
COM_TE = (1 << 2)
};

u8 rtl8139_tbuff[4][TX_BUFF_SIZE] __attribute__((aligned(256)));

struct rtl8139_regs {
u8 mac[6];
u8 pad0[10];
u32 tsd[4];
u32 tsad[4];
u8 pad1[7];
u8 com;
u8 pad2[26];
u8 config1;
};

static_assert( offsetof(struct rtl8139_regs, tsad) == 0x20);
static_assert( offsetof(struct rtl8139_regs, tsd) == 0x10);
static_assert( offsetof(struct rtl8139_regs, com) == 0x37);
static_assert( offsetof(struct rtl8139_regs, config1) == 0x52);

volatile struct rtl8139_regs *rtl_regs;

u64 g_regs_addr;
u64 g_cur_buf = 0;

bool mm_paging_walk(uaddr va, paddr *pte, bool alloc);

u32 walkaddr(uaddr addr) {
paddr pte_addr;
u64 pte;
u32 off;

off = addr & 0xfff;
addr &= (~0xfffUL);

assert(mm_paging_walk(addr, &pte_addr, false));

copy_from_physical(&pte, pte_addr, sizeof pte);

pte = (pte >> 10) << 12;
assert(pte < U32_MAX);
return (u32) pte | off;
}

bool rtl8139_send_packet(u8 *buffer, u32 len) {
u32 phys_buffer = walkaddr((uaddr) (&rtl8139_tbuff[0]));
u64 i;

print("{u32:x}", phys_buffer);

for (i=0; i<len; ++i) {
rtl8139_tbuff[0][i] = buffer[i];
}

if ((rtl_regs->tsd[0] & TSD_OWN) == 0) {
return false;
}

rtl_regs->tsad[0] = phys_buffer;
rtl_regs->tsd[0] = len;

++g_cur_buf;
return true;
}

bool eth_send_packet(u8 *buffer, u32 len) {
u8 buf[0x100];
u64 i;

for (i=0; i<6; ++i) {
buf[i] = 0xff;
buf[i + 6] = rtl_regs->mac[i];
}
buf[12] = 0x08;
buf[13] = 0x00;

for (i=0; i<len; ++i) {
buf[i + 14] = buffer[i];
}
for (i=len; i<64; ++i) {
buf[i + 14] = 0;
}
if (len < 64) len = 64;

return rtl8139_send_packet(buf, len + 13);
}

void rtl8139_test() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hm, my first instinct would be to say "make this a selftest," but we don't want selftests that depend on the user having certain hardware either...

This probable deserves some thought.

Should this be exposed to userspace, so we can hit it in CI (maybe this would be a build time option, see #62)? Should we accumulate a list of selftests for devices (not drivers) and allow this to be run later in boot?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I tried a selftest and realized that myself, I had thought of some sort of devices selftest, but I don't want to create too many different types of selftest, so I would be in favor of exposing to userspace and having some userspace testing

eth_send_packet((u8*) "yellow submarine", 16);

assert(rtl_regs->tsad[0] != 0);
assert((rtl_regs->tsd[0] & TSD_TOK) != 0);
}

void rtl8139_init(struct pci_device *device) {
print("initializing rtl8139");

u32 reg_addr = 0x40000000;
g_regs_addr = reg_addr;

uaddr lo, hi;
struct vma *vma;
vma = vma_alloc(&kernel_virtual_allocator, 1);
assert(vma);

vma_bounds(vma, &lo, &hi);

mm_paging_map(lo, paddr_of_bits(reg_addr), PGPERM_KRW);

rtl_regs = (struct rtl8139_regs*) lo;



/* TODO: Maybe move PCI init to pci.c? */
/* Setup BAR for MMIO - we just pick a nice number for now */
device->memar = reg_addr;
device->cmd |= 0x6;

rtl_regs->config1 = 0;

rtl_regs->com = COM_RST;
while (rtl_regs->com & COM_RST) ;
rtl_regs->com = COM_RE | COM_TE;

rtl_regs->tsad[0] = 0x12;
assert(rtl_regs->tsad[0] == 0x12);

rtl8139_test();
}
2 changes: 2 additions & 0 deletions src/kernel/include.mak
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ kernel-objs-c += builtins/strlen
kernel-objs-c += crypto/subtle/rfc7539
kernel-objs-c += crypto/subtle/rfc7693
kernel-objs-c += devicetree
kernel-objs-c += drivers/rtl8139
kernel-objs-c += main
kernel-objs-c += mm/alloc
kernel-objs-c += mm/alloc/block
Expand All @@ -41,6 +42,7 @@ kernel-objs-c += mm/alloc/segment
kernel-objs-c += mm/physical_alloc
kernel-objs-c += mm/virtual_alloc
kernel-objs-c += panic
kernel-objs-c += pci
kernel-objs-c += print
kernel-objs-c += random
kernel-objs-c += selftest
Expand Down
11 changes: 11 additions & 0 deletions src/kernel/include/drivers/rtl8139.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef UKO_OS_KERNEL__RTL8139_H
#define UKO_OS_KERNEL__RTL8139_H 1

#include <types.h>
#include <pci.h>

void rtl8139_init(struct pci_device *device);
void rtl8139_test();
bool eth_send_packet(u8 *buffer, u32 len);

#endif
1 change: 1 addition & 0 deletions src/kernel/include/mm/physical_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define UKO_OS_KERNEL__MM_PHYSICAL_ALLOC_H 1

#include <types.h>
#include <devicetree.h>

/**
* Initializes the physical allocator with the memory discovered in the
Expand Down
19 changes: 19 additions & 0 deletions src/kernel/include/pci.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef UKO_OS_KERNEL__PCI_H
#define UKO_OS_KERNEL__PCI_H 1

#include <types.h>


void pci_enumerate();

struct pci_device {
u16 vid;
u16 did;
u8 cmd;
u8 pad[15];
u32 memar;
};
static_assert( offsetof(struct pci_device, memar) == 0x14);


#endif
4 changes: 4 additions & 0 deletions src/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <random.h>
#include <selftest.h>
#include <symbolicate.h>
#include <drivers/rtl8139.h>
#include <pci.h>

[[noreturn]]
void main(u64 hart_id, paddr devicetree_start, paddr kernel_start,
Expand Down Expand Up @@ -46,6 +48,8 @@ void main(u64 hart_id, paddr devicetree_start, paddr kernel_start,

print("Running self-tests...");
run_selftests();
pci_enumerate();
//rtl8139_test();

TODO();
}
40 changes: 40 additions & 0 deletions src/kernel/pci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <pci.h>
#include <print.h>
#include <mm/paging.h>
#include <mm/virtual_alloc.h>
#include <physical.h>

#include <drivers/rtl8139.h>

constexpr u64 pci_config = 0x30000000L;

void pci_enumerate() {
uaddr lo, hi;
struct vma *vma;

vma = vma_alloc(&kernel_virtual_allocator, 32);
assert(vma);

vma_bounds(vma, &lo, &hi);

for (u64 i=0; i<32; ++i) {
mm_paging_map(lo + i * 0x1000, paddr_of_bits(pci_config + i * 0x1000), PGPERM_KRW);
}
for (u64 dev=0; dev<32; ++dev) {
u64 bus = 0;
u64 func = 0;
u64 offset = 0;
offset = (bus << 16) | (dev << 11) | (func << 8) | (offset);
struct pci_device *device = (struct pci_device *)(lo + offset);

u16 did = device->did;
u16 vid = device->vid;

if (vid == 0x10ec && did == 0x8139){ //0xedf11efd) {
rtl8139_init(device);
break;
} else if (did != 0xffff && vid != 0xffff && vid) {
print("unknown pci device: {u16:x} vender: {u16:x}", did, vid);
}
}
}
Loading