본문 바로가기
pintos

Appendix A.1 Loading 정리

by 메릴린 2022. 12. 9.
728x90

STEP0: run threads/loader.S

  1. PC BIOS 가 loader(MBR)를 메모리에 올림 (하드디스크의 첫번째 sector에)
  2. loader는 하드디스크의 partition table에서 kernel bootable partition을 찾는다.
  3. loader reads the partition's contents into memory at physical address 128kB
  4. loader extracts the entry point(kernel ELF header에 포함된) from the loaded kernel and transfer control to it.

Transfer control to the kernel that we loaded. We read the start address out of the ELF header (see [ELF1]) and convert it from a 32-bit linear address into a 16:16 segment:offset address for real mode, then jump to the converted address. The 80x86 doesn't have an instruction to jump to an absolute segment:offset kept in registers, so in fact we store the address in a temporary memory location, then jump indirectly through that location. To save 4 bytes in the loader, we reuse 4 bytes of the loader's code for this temporary pointer.

mov $0x2000, %ax
mov %ax, %es
mov %es:0x18, %dx
mov %dx, start
movw $0x2000, start + 2
ljmp *start

⇒ start() in threads/start.S

STEP1: run threads/start.S

Low-Level Kernel Initialization

Loader's CPU initialization

  1. BIOS로부터 PC memory size를 가져와 machine's memory size를 가져온다.
  2. enables the A20 line, which is the CPU's address line numbered 20
  3. creates a basic page table
  4. loads the CPU's control registers to turn on protected mode and paging, and set up the segment registers.
  5. call main() in threads/init.c

STEP2: main()

main() 의 내용을 실행하는데

  1. clear kernel’s “BSS” : initialize segments to all zeros
  2. thread_init() 실행을 통해 main thread를 생성하고 해당 thread를 RUNNIG 상태로 바꾼다. 이때 priority는 PRI_DEFAULT
  3. console init
  4. initialize memory system
  5. initialize interrupt handler
  6. Start thread scheduler and enable interrupts.
    • idle_started semaphore 0으로 초기화
    • idle thread 초기화 이때 priority는 PRI_MIN → thread_create("idle", PRI_MIN, idle, &idle_started) 함수 실행
    • start preemptive thread scheduling
    • wait for the idle thread to initalize idle_thread : sema_down(&idle_started)
    • → thread_create함수에 넘어가는 idle함수에서 sema_up(&idle_started) 을 실행한다.serial_init_queue()와 timer_calibrate() 함수까지 마저 실행
  7. thread_start()
  8. 상황에 따라 USERPROG나 FILESYS가 필요할 경우 추가 initializing 작업 완료하면 Boot complete
  9. run_action(argv) 함수를 실행해 사용자가 입력한 command에 맞게 action(test or user program) 실행
  10. action 실행완료 후 shutdown() 또는 thread_exit() 함수 실행→ 다른 경우, thread_exit()를 실행해 다른 thread를 실행할 수 있도록 한다.
  11. → ‘-q’ option을 넣어 command를 작성했을 경우 shutdown_power_off() 함수를 실행해 machine simulator를 종료

Physical Memory Map

pintos manual에서 발췌

 

728x90
반응형

댓글