bos & network driver practice

36
創創 CC BOS & Network Driver Practice Ben Wei (A system creator) JULUOSDev 創創創創 TOSSUG

Upload: courtney

Post on 22-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

T OSSUG. JULUOSDev 星系主題. BOS & Network Driver Practice. Ben Wei (A system creator). Agenda. BOS 簡介 lspci e100_attach T ransmit & Receive Demo Issues Q emu OSE. Agenda. BOS 簡介 lspci e100_attach T ransmit & Receive Demo Issues Q emu OSE. Bos 簡介. Bos ( a.k.a Ben’s OS) 目前實作 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: BOS & Network Driver Practice

創用 CC

BOS & Network Driver Practice

Ben Wei(A system creator)

JULUOSDev星系主題

TOSSUG

Page 2: BOS & Network Driver Practice

創用 CC

Agenda• BOS 簡介• lspci• e100_attach• Transmit & Receive• Demo• Issues• Qemu OSE

2

Page 3: BOS & Network Driver Practice

創用 CC

Agenda• BOS 簡介• lspci• e100_attach• Transmit & Receive• Demo• Issues• Qemu OSE

3

Page 4: BOS & Network Driver Practice

創用 CC

Bos 簡介

Bos (a.k.a Ben’s OS)目前實作• 簡易的記憶體管理• Timer中斷處理• 鍵盤 I/O, 佇列• 多個執行緒的多工切換• 精簡 blibc 函式庫• bshell 簡單的命令模式 (ps, uname, free, clear…)

4

Page 5: BOS & Network Driver Practice

創用 CC

Architecture

5

CU RU

Kernel

E100 i82559ER

PCIFunc

TimerKernel

Kthread(Bshell)

Mem

lspci net

Network Driver

Page 6: BOS & Network Driver Practice

創用 CC

Agenda• BOS 簡介• lspci• e100_attach• Transmit & Receive• Demo• Issues• Qemu OSE

6

Page 7: BOS & Network Driver Practice

創用 CC

lspci

• OSDev.org “here I can find Programming Info on PCI?”

– sysInLong: 讀取 pci 表內容– sysOutLong: 寫入 pci 設備命令及資料

7

Page 8: BOS & Network Driver Practice

創用 CC

MIT OSE x86.h

• uint32_t inl(int port)

   __asm __volatile("inl %w1,%0" : "=a" (data) : "d" (port));

• void outl(int port, uint32_t data)

   __asm __volatile("outl %0,%w1" : : "a" (data), "d" (port));

8

Page 9: BOS & Network Driver Practice

創用 CC

inl/outl NASM;uint32_t _intl(int port)_inl:        mov  edx, [esp+4]        mov  eax, 0        in eax, dx        ret;void _outl(int port, uint32_t data)_outl:        mov  edx, [esp+4]  ; port        mov  eax, [esp+8]  ; data        out  dx, eax        ret

9

Page 10: BOS & Network Driver Practice

創用 CC

PCI Config Read

pciConfigRead(uint32_t bus, uint32_t slot, uint32_t func, uin32_t offset)address = (uint32_t)((bus << 16) | (slot << 11) | (func << 8) | (offset & 0xfc) | ((uint32_t)1<<31)); outl(0xCF8, address);data = (uint32_t)(intl(0xCFC);

10

31 30-24 23-16 15-11 10-8 7-2 1-0

Eable Bit Reserved Bus Num Device Num Func Num Reg Num 00

Page 11: BOS & Network Driver Practice

創用 CC

Ethernet Controller Info00:18.0 Vendor:8086, devid:1029, class:0200h(Ethernet Controller),r9,t0,irq11

11

Register 31-24 23-16 15-8 7-0

00 Device ID Vendor ID

04 Status Command

08 Class No SubClass No Prog IF Reversion ID

0C BIST Header Type

Latency Timer Cache Line Size

3C Mac Latency Min Grant Interrupt PIN Interrupt Line

Page 12: BOS & Network Driver Practice

創用 CC

Agenda• BOS 簡介• lspci• e100_attach• Transmit & Receive• Demo• Issues• Qemu OSE

12

Page 13: BOS & Network Driver Practice

創用 CC

E100_attach 啟用流程

• 啟用後,這時便用使用到 io region 1: 0xc040,長度為64位元組,參考 Intel 82559

• 需要將 e100 做軟體重置– e100_reset()

• Disable CSR 中斷

• DMA Rings– 傳送的緩衝區 Control Block List (CBL): cbl_init()– 接收用的緩衝區 Receive Frame Area (RFA): rfa_init()

13

Page 14: BOS & Network Driver Practice

創用 CC

PCI enable e100 func

• pci_func_enable(pci_pdata_t f)

pci_conf_write(f, PCI_COMMAND_STATUS_REG,                       PCI_COMMAND_IO_ENABLE |                       PCI_COMMAND_MEM_ENABLE |                       PCI_COMMAND_MASTER_ENABLE);

執行結果PCI function 00:18.0 (8086:1209) enabled

14

Page 15: BOS & Network Driver Practice

創用 CC

E100 Reset

void e100_reset(){ outl(e100.addr_base[E100_IO] + CSR_PORT, PORT_SW_RESET); delay(10);}

15

Page 16: BOS & Network Driver Practice

創用 CC

Tricky delay()

__inline void delay(int i){ while(--i >=0) { inb(0x84); }}

16

Page 17: BOS & Network Driver Practice

創用 CC

Disable e100 interrupt

r = e100_exec_cmd(CSR_INT, 1); printf("e100 CSR_INT ret=%d\n", r);

• e100_exec_cmd (int csr_comp, uint8_t cmd)

outb(nic.io_base + csr_comp, cmd); do { scb_command = inb(nic.io_base + CSR_COMMAND); } while (scb_command != 0 && --retry > 0); return retry > 0;

17

Page 18: BOS & Network Driver Practice

創用 CC

DMA Rings

• Control Block

• Control Block List (CBL)

• Receive Frame Area (RFA)

18

Page 19: BOS & Network Driver Practice

創用 CC

Control Block (CB)Control Status

Link

Command Specific Data

19

Control Status

Link

TBD ARRAY ADDR

TBD Count THRS TCB Byte Count

Data

Transmit Control Block (TCB)

Page 20: BOS & Network Driver Practice

創用 CC

DMA Rings for TCBs

20

Control Status

Link

0XFFFFFFFF

0 0xE0 Size

Packet Data

Control Status

Link

0XFFFFFFFF

0 0xE0 Size

Packet Data

Control Status

Link

0XFFFFFFFF

0 0xE0 Size

Packet Data

Page 21: BOS & Network Driver Practice

創用 CC

Agenda• BOS 簡介• lspci• e100_attach• Transmit & Receive• Demo• Issues• Qemu OSE

21

Page 22: BOS & Network Driver Practice

創用 CC

transmit & receive

• Send Packet data (transmit & receive)int e100_transmit (const char *data, uint16_t len)

• Receive packet dataint e100_receive (char *data)

22

Page 23: BOS & Network Driver Practice

創用 CC

Agenda• BOS 簡介• lspci• e100_attach• Transmit & Receive• Demo• Issues• Qemu OSE

23

Page 24: BOS & Network Driver Practice

創用 CC

Demo

24

Page 25: BOS & Network Driver Practice

創用 CC

Demo - recap

bos$ netPCI function 00:18.0 (8086:1209) enabledmem region 0: 4096 bytes at 0xf2020000io region 1: 64 bytes at 0xc040cbl avail = 9, wait = 1, slot = 0x420000, irq=11rfa avail = 10, wait = 0, slot = 0x420050

25

Page 26: BOS & Network Driver Practice

創用 CC

Agenda• BOS 簡介• lspci• e100_attach• Transmit & Receive• Demo• Issues• Qemu OSE

26

Page 27: BOS & Network Driver Practice

創用 CC

Issues

• 原使用 e100設備序號,並不生效;後來研究 OSE資料後,發現必須在 qemu參數中設定,之後再使用設備序號1209來啟用 e100(i82559er),如此才能正確執行在qemu環境。

• 系統在開發的過程中,偶遇到無法正常開機,這時 GIT 便可派上用場,來縮小問題發生的範圍。

27

Page 28: BOS & Network Driver Practice

創用 CC

Agenda• BOS 簡介• lspci• e100_attach• Transmit & Receive• Demo• Issues• Qemu OSE

28

Page 29: BOS & Network Driver Practice

創用 CC

編繹Qemu OSE版本

$ configure --disable-sdl –prefix=/usr/local/qemuose $ --target-list=i386-softmmu$ make$ sudo make install

另外在 Bos v0.21 中執行 Qemu 時,將檢查 /usr/local/qemuose ,若存在則直接使用。

29

Page 30: BOS & Network Driver Practice

創用 CC

Qemu 參數

• 參數 qemu -fda "../bos.img" -net user -net nic,model=i82559er

• MIT OSE 參數 ( 只在 qemu 0.12.5-6.828 中支援)-debug-e100-pcap <file>

30

Page 31: BOS & Network Driver Practice

創用 CC

結論

• PCI 資訊的存取• 啟用 e100 網路卡• 設定並使用 DMA Rings• 更好的偵錯方式來進行網卡開發• 相關遇到問題的經驗分享

31

Page 32: BOS & Network Driver Practice

創用 CC 32

Q & A

Page 33: BOS & Network Driver Practice

創用 CC

JuluOSDev議程

• 有興趣一起開發 Juluos的朋友們,請加入到 juluosdev group at google,參與開發議程討論,位置詳見Julu.staros.mobi 公告。

33

Page 34: BOS & Network Driver Practice

創用 CC

Backup Slides

34

Page 35: BOS & Network Driver Practice

創用 CC

Glossary of Abbreviations

• CB Control Block• CBL Command Block List• CSR Control/Status Registers• CU Command Unit• RFA Receive Frame Area• RFD Receive Frame Descriptor• RU Receive Unit• SCB System Control Block• TCB Transmit Command Block

35