2013年1月20日 星期日

Lab1 筆記

Linker用的 address

VMA (Link address)
    表示Linker在鍊結symbol table時所採用的base address.
    也就是程式希望運行的記憶體位置

LMA (load address)
    表示希望被讀到實體記憶體的哪個位置


Assembly 語法

AT&T syntax
mov %ax,%bx
 -> 將ax的值存入bx, 即可說AT&T格式是 OP src des

x:

      .long   1
      .long   7
      .long   9
      .long   14
-> 宣告一個空間, 由4個long佔據, 其初值為1, 7, 9, 14, 組語可用label x
   來存取到這個空間 
 

GDB 指令

x/Ni addr : 由addr連續印出N行組語指令, addr可用$eip
x/Nx addr : 由addr連續印出N組記憶體內容

si: 單步執行下一個組語指令
s:  單步執行下個c指令n:  執行下個c指令(執行完完整function)
c:  執行程式
b:  設break point
     *addr -> 在 address處設 break point

i r: info registers的縮寫, 可以觀看CPU registers的情形
p var_name : 印出變數內容

list: 列出目前 c程式碼
quit: 結束 

Print Section

    va_start, va_arg藉由直接存取被compiler放置於stack中的參數來達到,
可以讓Function可以傳入任意長度的參數.

挑戰題

參考文章
VGA Color Palette
ASCII Escape Rule
設計想法:
  1. 將Escape的顏色轉換成 VGA format, 丟給VGA來顯示顏色
  2. 在vprintfmt中建立Escape rule的parser


-- 未完 --

JOS bootloader 心得

今天總算把bootloader研究完了, 做些重點整理

#include <inc/mmu.h>

# Start the CPU: switch to 32-bit protected mode, jump into C.
# The BIOS loads this code from the first sector of the hard disk into
# memory at physical address 0x7c00 and starts executing in real mode
# with %cs=0 %ip=7c00.

.set PROT_MODE_CSEG, 0x8         # kernel code segment selector
.set PROT_MODE_DSEG, 0x10        # kernel data segment selector
.set CR0_PE_ON,      0x1         # protected mode enable flag

.globl start
start:
  .code16                # Assemble for 16-bit mode
  cli                         # Disable interrupts
  cld                        # String operations increment

  # Set up the important data segment registers (DS, ES, SS).
  xorw    %ax,%ax             # Segment number zero
  movw    %ax,%ds             # -> Data Segment
  movw    %ax,%es             # -> Extra Segment
  movw    %ax,%ss             # -> Stack Segment

  # Enable A20:                 
  #   For backwards compatibility with the earliest PCs, physical
  #   address line 20 is tied low, so that addresses higher than
  #   1MB wrap around to zero by default.  This code undoes this.
  #   LL: Study book P.84
  #  為了和舊PC相容, 需要下列動做讓address大於bit20部分可以運作
seta20.1:
  inb     $0x64,%al               # Wait for not busy
  testb   $0x2,%al
  jnz     seta20.1

  movb    $0xd1,%al               # 0xd1 -> port 0x64
  outb    %al,$0x64

seta20.2:
  inb     $0x64,%al               # Wait for not busy
  testb   $0x2,%al
  jnz     seta20.2

  movb    $0xdf,%al               # 0xdf -> port 0x60
  outb    %al,$0x60

  # Switch from real to protected mode, using a bootstrap GDT
  # and segment translation that makes virtual addresses
  # identical to their physical addresses, so that the
  # effective memory map does not change during the switch.
  lgdt    gdtdesc                # LL: I can not understand!!!
  movl    %cr0, %eax
  orl     $CR0_PE_ON, %eax
  movl    %eax, %cr0
 
  # Jump to next instruction, but in 32-bit code segment.
  # Switches processor into 32-bit mode.
  #  透過這行才會讓 CS register讀入正確的gdt offset
  #  $PROT_MODE_CSEG為 0X8, 因為 0X0為SEG_NULL,
  #  偏移8才為 SEG_CODE

  ljmp    $PROT_MODE_CSEG, $protcseg

  .code32                     # Assemble for 32-bit mode
protcseg:
  # Set up the protected-mode data segment registers
  movw    $PROT_MODE_DSEG, %ax    # Our data segment selector
  movw    %ax, %ds                # -> DS: Data Segment
  movw    %ax, %es                # -> ES: Extra Segment
  movw    %ax, %fs                # -> FS
  movw    %ax, %gs                # -> GS
  movw    %ax, %ss                # -> SS: Stack Segment
 
  # Set up the stack pointer and call into C.
  # Stack register設為 $start(0x7c00 code開始位置),
  # 因為stack是往低位址跑, 所以不會和code相撞
  movl    $start, %esp
  call bootmain

  # If bootmain returns (it shouldn't), loop.
spin:
  jmp spin

# Bootstrap GDT
.p2align 2                                # force 4 byte alignment
gdt:
  SEG_NULL                # null seg
  SEG(STA_X|STA_R, 0x0, 0xffffffff)    # code seg
  SEG(STA_W, 0x0, 0xffffffff)            # data seg 

  # 這寫法指 gdtdesc為一個struct有兩個member, 一個為word, 一個為long
  # word是指gdt的size(0代表1byte, 所以才要減一)
  # long是指由哪個位址讀取 gdt table gdtdesc:
  .word   0x17                            # sizeof(gdt) - 1
  .long   gdt                             # address gdt


C code部分比較簡單, 先讀4K的 Kernel(由Sector1開始, sector0是boot loader)放到
0x10000.
再來根據ELF的格式, 把各個segmentload對應的
physical  address. 即可跳入Kernel.

2013年1月16日 星期三

Ext2 memo

儲存裝置被切割為一個個的block, 單位為1K, 2K, or 4K

Ext2 format時切割出一個個的 Block group.
其中可分為六個區段
  1. Superblock
    記錄和整個file system相關資訊, 如
    Inode, block的大小; Inode可用數量, blocks可用數量...等
  2. Filesystem Description
    描述Block group使用哪個block到哪一個block,
    每個區段又是從哪個block到哪個block
  3. block bitmap
    記錄每個block是否有被使用
  4. inode bitmap
    記錄每個Inode是否有被使用
  5. Inode talbe
    放置這個檔案系統的 Inodes
  6. Data blocks
    放置檔案blocks的所在

  • Inode
    一個Inode為128bytes, 用來紀錄一個檔案的相關資訊,
    還有指向所有屬於這個檔案的data blocks.
  • Directory
    和檔案一樣會耗掉一個Inode, 其data block中放置屬於該folder的
    檔名與Inode對應表
  • Hard Link
    將產生hard link的folder之對應data block中新增一項
    檔名(Hard link)與Inode的對應
  • Soft Link
    產生一個新檔案(會耗費一個Inode), 其data block記錄所連結的檔案
    Ex: ln -s /myTarget abc,
          則abc的data block中會寫著/myTarget

2013年1月9日 星期三

Vim plugin memo

Cscope

  • Install and Setup
    sudo apt-get install cscope
    mkdir -p ~/.vim/plugin
    cd ~/.vim/plugin
    wget http://cscope.sourceforge.net/cscope_maps.vim
  • Build project
    cd PROJECT
    cscope -bR

2013年1月8日 星期二

Learning MIT 6.828 2011 Fall

    偶然間發現MIT 6.828的公開課程(MIT6.828 2011 Fall).
 想起自己對OS的熱愛, 試著在這紀錄學習的歷程.

程式碼放置在Github : NEOS

這篇文章記錄環境的設定.

1. Install Virtual Box 4.26

2. Install Ubuntu  12.10

3. Install Git

4. Install Vim

5. Setup xv6OS(offical link)

6. Install MIT version QEUM(Link)
    - 安裝在 /usr/local 才能跑, 安裝在~/中不能跑, 沒有進一步除錯.

7. type "make qeum" under xv6OS folder and simulating environment starts running :)


[Lab1]
- Exercise.2
  將.gdbinit 由 xv6OS folder 複製到 ~ 下來讓gdb可以正常運作.

其餘代補.

簡序

  創了一個網誌, 來放些和程式學習的軌跡.

  Creating a blog for recording the track of Learning programming.