Chapter 0 : 一些你可能正感到迷惑的問題 === :::info 這是讀書筆記 ![book](https://hackmd.io/_uploads/H1rFFZZAkx.jpg =30%x) 作者:鄭鋼 出版社:佳魁資訊股份有限公司 出版日期:2017/05/31 ::: --- # 心得筆記 ## 作業系統是什麼 社會組織和人們的關係,就是作業系統和 user process 的關係。 ## 你想研究到什麼程度 把硬體當成黑盒子就好。 ## 撰寫作業系統,哪些需要我來做 作業系統是管理硬體的軟體,所以需要了解硬體提供的介面,以便操控硬體。 ## 軟體是如何存取硬體的 ==這邊精準的說,應該是 CPU 如何去存取外部硬體。== * Memory-mapped I/O * Port I/O ## 應用程式是什麼,和作業系統是如何配合到一起的 * CPU 並沒有辦法分辨應用程式還是作業系統,都是一道道指令罷了。 (這邊先不談CPU模式切換) * 應用程式是透過***系統呼叫***來跟作業系統溝通的。 ## 為什麼稱為「陷入」核心 特權等級的不同。 ## 記憶體存取為什麼要分段 可以讓 16 bits CPU registers (Intel 8086: cs:ip) 存取 1MB(20 bits) 記憶體。 ## 程式中為什麼分成程式碼片段、資料段? Intel CPU cs:ip 能夠自動獲得下一行指令的原理是:eip+目前指令的長度。 沒有異常的話他會執行到世界末日。 因此把程式碼指令和資料分開,對於 Intel CPU 執行比較有效率。 再者,如果將其放在不同記憶體區段中,可以分開設置權限。(OS 透過設置 GDT) 記憶體分段: CPU 為了存取記憶體的機制。 程式分段:人為邏輯劃分的記憶體區域。 ## 實體位址、邏輯位址、有效位址、線性位址、虛擬位址的區別 CPU 最終都是要用實體位址存取記憶體。 線性位址: * without paging : 實體位址。 * with paging : 虛擬位址。 有效位址=邏輯位址:section 內的 offset。 ## 什麼是段重疊 不重要。 ## 什麼是平坦模型 不重要。 ## cs、ds 這種 sreg 段暫存器,位寬是多少 16 bits ## 什麼是專案,什麼是協定 不重要。 ## 為什麼Linux系統下的應用程式不能在Windows系統下執行 不重要。 ## 區域變數和函數參數為什麼要放在 stack 中 不重要。 ## 為什麼說組合語言比C語言快 跟編譯器有關。 ## 先有的語言,還是先有的編譯器 先有編譯器。 ## 編譯型程式與直譯型程式的區別 直譯型程式會跑在解譯器上。 ## 什麼是 big endian and little endian 太基礎了。 先放MSB的就是 big endan;先放LSB的就是 little endian。 ## BIOS中斷、DOS中斷、Linux中斷的區別。 BIOS and DOS 透過 Interrupt Vector Table(IVT)找到中斷處理程式。 > The interrupt vector table is normally located in the first 1024 bytes of memory at addresses 000000H–0003FFH. It contains 256 different interrupt vectors. Each vector is 4 bytes long and contains the starting address of the ISR. Linux 透過 Interrupt Descriptor Table(IDT)找到中斷處理程式。 ## Section and Segment 的區別 多個 section 集合成一個 Segment。(同個屬性) ## 什麼是 Magic Number 不重要。 ## 作業系統如何識別檔案系統 不重要。 ## 如何控制CPU下一行指令 Intel : jmp, call, int, ret. ARM : 直接修改 pc ## 指令集、系統架構、微架構、程式語言 CISC : intel x86 RISC : ARM MIPS Power ## 函式庫是使用者處理程序與核心的橋樑 不重要。 ## 跳脫字元與ASCII Code 不重要。 ## MBR、EBR、DBR、OBR各是什麼 BIOS -> MBR -> Bootloader -> OS MBR 位於整個硬碟最開始的磁區。 OBR : OS boot record, 作業系統啟動磁區。 DBR : OBR 的舊稱。 EBR : Expand boot record >[!Note]詳細概念 等之後研究硬碟吧。 # 熱心網友的 source code >參考 Github:[《操作系统真象还原》札记](https://github.com/yifengyou/os-elephant) >[!Tip] [簡中轉繁中的工具](https://www.ifreesite.com/gbk-big5-gb2312-utf8.htm) 可以看看他的說明文字,真的超級熱血! # 我想要補充的背景知識 ## 從零開發作業系統 https://www.youtube.com/watch?v=imbeV-XPix4&list=PLqhmiqnOBz99tJpJZD5z60VKtgB7to-w1 ## 開發 ARM OS https://github.com/Joyounger/leeos/blob/master/leoos(jb51.net).pdf https://github.com/jserv/mini-arm-os ## CPU 架構的基本概念 ## x86 CPU Registers/Instructions/Memory Map ## ARM CPU Registers/Instructions/Memory Map ## Assembly Language ## x86 Boot Sequence ## ARM Boot Sequence >參考資料:[Booting Linux](https://developer.arm.com/documentation/den0013/d/Boot-Code/Booting-Linux?lang=en) :::spoiler Booting Linux It is useful to understand what happens from the core coming out of reset and executing its first instruction at the exception base address `0x00000000` or `0xFFFF0000` if `HIVECS` (known as high vectors) is selected, until the Linux command prompt appears. (See The Vector table.) When the kernel is present in memory, the sequence on an ARM processor based system is similar to what might happen on a desktop computer. However, the bootloading process can be very different, as ARM processor based phones or more deeply embedded devices can lack a hard drive or PC-like BIOS. Typically, what happens when you power the system on is that hardware specific boot code runs from flash or ROM. This code initializes the system, including any necessary hardware peripheral code and then launches the bootloader (for example U-Boot). This initializes main memory and copies the compressed Linux kernel image into main memory (from a flash device, memory on a board, MMC, host PC or elsewhere). The bootloader passes certain initialization parameters to the kernel. The Linux kernel then decompresses itself and initializes its data structures and running user processes, before starting the command shell environment. Let's take a more detailed look at each of those processes. ::: ## ARM ROM code example >參考資料:[Sample code](https://developer.arm.com/documentation/dui0056/d/writing-code-for-rom/loading-the-rom-image-at-address-0/sample-code?lang=en) >Onpen Source Project:[ARMboot](https://armboot.sourceforge.net/) ## What's UEFI ## [Bcohs BIOS source code](https://github.com/ipxe/bochs/blob/master/bios/rombios32.c) ## [SeaBIOS](https://www.seabios.org/downloads/) ## 使用 qemu 啟動 ARM 虛擬機 >use STM32 https://github.com/jserv/mini-arm-os https://beta.hackfoldr.org/arm ## 網友補充的設定 bochs https://blog.csdn.net/qq_61653333/article/details/136962598 ## 80386 手冊 https://www.scs.stanford.edu/05au-cs240c/lab/i386/toc.htm https://pdos.csail.mit.edu/6.828/2018/readings/i386.pdf