README.adoc 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455
  1. = x86 Bare Metal Examples
  2. :idprefix:
  3. :idseparator: -
  4. :sectanchors:
  5. :sectlinks:
  6. :sectnumlevels: 6
  7. :sectnums:
  8. :toc: macro
  9. :toclevels: 6
  10. :toc-title:
  11. Dozens of minimal operating systems to learn x86 system programming. Tested on Ubuntu 18.04 host in QEMU 2.11 and <<test-hardware,real hardware>>. Userland cheat at: https://github.com/cirosantilli/linux-kernel-module-cheat#userland-assembly ARM baremetal setup at: https://github.com/cirosantilli/linux-kernel-module-cheat#baremetal-setup
  12. :logo: logo.jpg
  13. link:{logo}[image:{logo}[]]
  14. toc::[]
  15. == China
  16. Before <<getting-started>>, read this important pre-requisite: https://github.com/cirosantilli/china-dictatorship
  17. Because human rights is more important than profit and technology.
  18. image::https://raw.githubusercontent.com/cirosantilli/china-dictatorship-media/master/Hao_Haidong_kick.jpg[width=600]
  19. image::https://raw.githubusercontent.com/cirosantilli/china-dictatorship-media/master/Xi_sadomasochist.jpg[width=600]
  20. == Getting started
  21. First read this introduction: https://stackoverflow.com/questions/22054578/how-to-run-a-program-without-an-operating-system/32483545#32483545
  22. Then on Ubuntu:
  23. ....
  24. ./configure
  25. make
  26. ....
  27. Each `.S` file on the top-level is an operating system! It gets compiled to a corresponding `.img` file.
  28. Run the default OS on QEMU:
  29. ....
  30. ./run
  31. ....
  32. Run a given OS:
  33. ....
  34. ./run bios_hello_world
  35. ./run bios_putc
  36. ....
  37. Examples described at:
  38. * <<bios-examples>>
  39. * <<bios-hello-world>>
  40. Extensions are ignored for perfect tab completion, so all the following are equivalent:
  41. ....
  42. ./run min
  43. ./run min.
  44. ./run min.S
  45. ./run min.img
  46. ....
  47. Use Bochs instead of QEMU:
  48. ....
  49. ./run bios_hello_world bochs
  50. ....
  51. Then on the terminal start the simulation with:
  52. ....
  53. c
  54. ....
  55. To quit Bochs either:
  56. * press the poweroff button inside its GUI
  57. * Ctrl + C on terminal and the type `quit` and hit enter
  58. TODO: automate this step.
  59. Bibliography: https://stackoverflow.com/questions/6142925/how-can-i-use-bochs-to-run-assembly-code/32871939#32871939
  60. === Getting started with real hardware
  61. Insert an USB, determine its device (`/dev/sdX`) with:
  62. ....
  63. sudo lsblk
  64. sudo fdisk -l
  65. ....
  66. Pick the `.img` file that you wan to run and:
  67. ....
  68. sudo dd if=bios_hello_world.img of=/dev/sdX
  69. ....
  70. Then:
  71. * insert the USB in a computer
  72. * during boot, hit some special hardware dependant key, usually F12, Esc
  73. * choose to boot from the USB
  74. When you are done, just hit the power button to shutdown.
  75. For example, on my T430 I see the following.
  76. After turning on, this is when I have to press Enter to enter the boot menu:
  77. image::https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/Lenovo_ThinkPad_T430_UEFI_BIOS_1.16_splash_screen.jpg/640px-Lenovo_ThinkPad_T430_UEFI_BIOS_1.16_splash_screen.jpg[]
  78. Then, here I have to press F12 to select the USB as the boot device:
  79. image::https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Lenovo_ThinkPad_T430_UEFI_BIOS_1.16_Startup_Interrupt_Menu.jpg/640px-Lenovo_ThinkPad_T430_UEFI_BIOS_1.16_Startup_Interrupt_Menu.jpg[]
  80. From there, I can select the USB as the boot device like this:
  81. image::https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Lenovo_ThinkPad_T430_UEFI_BIOS_1.16_Boot_Menu.jpg/640px-Lenovo_ThinkPad_T430_UEFI_BIOS_1.16_Boot_Menu.jpg[]
  82. Alternatively, to change the boot order and choose the USB to have higher precedence so I don't have to manually select it every time, I would hit F1 on the "Startup Interrupt Menu" screen, and then navigate to:
  83. image::https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Lenovo_ThinkPad_T430_UEFI_BIOS_1.16_boot_order_menu.jpg/640px-Lenovo_ThinkPad_T430_UEFI_BIOS_1.16_boot_order_menu.jpg[]
  84. See also: <<test-hardware>>
  85. ==== Getting started with the big image
  86. Create a `big.img` that contains all examples that can be booted from GRUB:
  87. ....
  88. make big.img
  89. ....
  90. Now if you do:
  91. ....
  92. sudo dd if=big.img of=/dev/sdX
  93. ....
  94. you can test several examples with a single USB burn, which is much faster.
  95. You can also try out the big image on QEMU for fun with:
  96. ....
  97. qemu-system-i386 -hda big.img
  98. ....
  99. You will also want to change the boot order to put the USB first from the F12 BIOS menu. This way you don't have to hit F12 like a madman every time.
  100. TODO: boot sectors that load STAGE2 are not working with the big image chainloader. TODO why?
  101. === Getting started with Docker
  102. If you don't have an Ubuntu box, this is an easy alternative, for the first run:
  103. ....
  104. sudo docker run --interactive --tty --name xbme --net=host ubuntu:18.04 bash
  105. ....
  106. and the following runs:
  107. ....
  108. sudo docker start xbme
  109. sudo docker exec --interactive --tty xbme bash
  110. sudo docker stop xbme
  111. ....
  112. and to nuke the container later on:
  113. ....
  114. # sudo docker rm xbme
  115. ....
  116. Then proceed normally in the guest: install packages, and build:
  117. ....
  118. apt-get update && \
  119. apt-get install -y git && \
  120. git clone https://github.com/cirosantilli/x86-bare-metal-examples && \
  121. cd x86-bare-metal-examples && \
  122. ./configure -y && \
  123. make
  124. ....
  125. To overcome the lack of GUI, we can use QEMU's VNC implementation instead of the default SDL, which is visible on the host due to `--net=host`:
  126. ....
  127. ./run bios_hello_world run -vnc :0
  128. ....
  129. and then on host:
  130. ....
  131. sudo apt-get install vinagre
  132. vinagre localhost:5900
  133. ....
  134. TODO: get sound working from docker: <<pc-speaker>>: https://stackoverflow.com/questions/41083436/how-to-play-sound-in-a-docker-container
  135. It should also be possible to run a GUI inside the container, but I haven't tested: https://stackoverflow.com/questions/40658095/how-to-open-ubuntu-gui-inside-a-docker-image/57636624#57636624
  136. === GDB step debug
  137. To GDB step debug the program, run it with:
  138. ....
  139. ./run bios_hello_world debug
  140. ....
  141. This will leave you at the very first instruction executed by our program, which is the beginning of our `BEGIN` macro.
  142. Note however that this is not the very first instruction QEMU executes: that will actually be BIOS setup code that runs before our program itself.
  143. You can then basically debug as you would a normal userland program, notably:
  144. * I then highly recommend that you use https://github.com/cyrus-and/gdb-dashboard[GDB Dashboard] to see what is going on.
  145. * `n` skips over macros
  146. * `ni` steps within macros. But you will need to enable the printing of assembly code on GDB Dashboard to see where you are at
  147. With this God-like GDB Dashboard setup, at 89cbe7be83f164927caebc9334bc42990e499cb1 I see a perfect program view such as:
  148. ....
  149. 1 /* https://github.com/cirosantilli/x86-bare-metal-examples#bios-hello-world */
  150. 2
  151. 3 #include "common.h"
  152. 4 BEGIN
  153. 5 mov $msg, %si
  154. 6 mov $0x0e, %ah
  155. 7 loop:
  156. 8 lodsb
  157. 9 or %al, %al
  158. 10 jz halt
  159. 11 int $0x10
  160. 12 jmp loop
  161. ─── Assembly ────────────────────────────────────────────────────────────────────────────
  162. 0x00007c00 __start+0 cli
  163. 0x00007c01 __start+1 ljmp $0xc031,$0x7c06
  164. 0x00007c08 __start+8 mov %eax,%ds
  165. 0x00007c0a __start+10 mov %eax,%es
  166. 0x00007c0c __start+12 mov %eax,%fs
  167. 0x00007c0e __start+14 mov %eax,%gs
  168. 0x00007c10 __start+16 mov %eax,%ebp
  169. 0x00007c12 __start+18 mov %eax,%ss
  170. 0x00007c14 __start+20 mov %ebp,%esp
  171. ─── Registers ───────────────────────────────────────────────────────────────────────────
  172. eax 0x0000aa55 ecx 0x00000000 edx 0x00000080 ebx 0x00000000 esp 0x00006f04
  173. ebp 0x00000000 esi 0x00000000 edi 0x00000000 eip 0x00007c00 eflags [ IF ]
  174. cs 0x00000000 ss 0x00000000 ds 0x00000000 es 0x00000000 fs 0x00000000
  175. gs 0x00000000
  176. ─── Stack ───────────────────────────────────────────────────────────────────────────────
  177. [0] from 0x00007c00 in __start+0 at bios_hello_world.S:4
  178. (no arguments)
  179. ─────────────────────────────────────────────────────────────────────────────────────────
  180. >>>
  181. ....
  182. Debug symbols are obtained by first linking ELF files, and then using `objcopy` on them to generate the final image. We then pass the ELF files with the debug information to GDB: https://stackoverflow.com/questions/32955887/how-to-disassemble-16-bit-x86-boot-sector-code-in-gdb-with-x-i-pc-it-gets-tr/32960272#32960272
  183. How to step over `int` calls: http://stackoverflow.com/questions/24491516/how-to-step-over-interrupt-calls-when-debugging-a-bootloader-bios-with-gdb-and-q
  184. Single stepping until a given opcode can be helpful sometimes: https://stackoverflow.com/questions/14031930/break-on-instruction-with-specific-opcode-in-gdb/31249378#31249378
  185. TODO: detect if we are on 16 or 32 bit automatically from control registers. Now I'm using 2 functions `16` and `32` to switch manually, but that sucks. The problem is that it's not possible to read them directly: http://stackoverflow.com/a/31340294/895245 If we had `cr0`, it would be easy to do with an `if cr0 & 1` inside a hook-stop.
  186. TODO: Take segmentation offsets into account: http://stackoverflow.com/questions/10354063/how-to-use-a-logical-address-in-gdb
  187. === Build the documentation locally
  188. ....
  189. make doc
  190. xdg-open README.html
  191. ....
  192. == Minimal examples
  193. These are the first ones you should look at.
  194. [[printf]]
  195. === Create a minimal image with printf
  196. ....
  197. make -C printf run
  198. ....
  199. Outcome: QEMU window opens up, prints a few boot messages, and hangs.
  200. Our program itself does not print anything to the screen itself, just makes the CPU halt.
  201. This example is generated with `printf` byte by byte: you can't get more minimal than this!
  202. It basically consists of:
  203. * byte 0: a `hlt` instruction
  204. * bytes 1 through 509: zeroes, could be anything
  205. * bytes 510 and 511: mandatory magic bytes `0xAA55`, which are required for BIOS to consider our disk.
  206. === Minimal GAS example
  207. Minimal example that just halts the CPU without using our mini-library link:common.h[]:
  208. ....
  209. ./run min
  210. ....
  211. Source: link:min.S[]
  212. Outcome: QEMU window opens up, prints a few firmware messages, and hangs.
  213. Here is an equivalent example using our mini-library:
  214. ....
  215. ./run template
  216. ....
  217. Source: link:template.S[]
  218. You can use that file as a quick template to start new tests.
  219. ==== Infinite loop
  220. Go into an infinite loop instead of using `hlt`:
  221. ....
  222. ./run infinite_loop
  223. ....
  224. Source: link:infinite_loop.S[].
  225. The outcome if visibly the same, but TODO: it likely wastes more energy in real hardware?
  226. ==== Linker script
  227. This hello world, and most of our OSes use the linker script: link:linker.ld[]
  228. This critical file determines the memory layout of our assembly, take some time to read the comments in that file and familiarize yourself with it.
  229. The Linux kernel also uses linker scripts to setup its image memory layout, see for example: https://github.com/torvalds/linux/blob/v4.2/arch/x86/boot/setup.ld
  230. === BIOS hello world
  231. Print `hello world` after the firmware messages:
  232. ....
  233. ./run bios_hello_world
  234. ....
  235. Source: link:bios_hello_world.S[]
  236. ==== C hello world
  237. Same output as <<bios-hello-world>>, but written in C:
  238. ....
  239. cd c_hello_world
  240. ./run
  241. ....
  242. Source: link:c_hello_world/[]
  243. But keep in mind the following limitations and difficulties:
  244. * single stage, so still limited to 512 bytes of code + data! TODO: it should be easy to solve that with <<bios-disk-load>>, send a pull request :-) Here is full example that we could also adapt: http://3zanders.co.uk/2017/10/18/writing-a-bootloader3
  245. * use use GCC's `-m` which does not produce "real" 16 bit code, but rather 32-bit code with `0x66` and `0x67` prefixes: https://wiki.osdev.org/X86-64_Instruction_Encoding#Legacy_Prefixes
  246. * setting up the initial state and the linker script is much harder and error prone than with assembly
  247. Therefore, for most applications, you will just want to use <<multiboot>> instead, which overcomes all of those problems.
  248. To disassemble the generated C code, try:
  249. ....
  250. objdump -D -m i8086 main.elf
  251. ....
  252. but note that it still contains references to 32-bit references, e.g.:
  253. ....
  254. 00007c17 <main>:
  255. 7c17: 66 55 push %ebp
  256. 7c19: 66 89 e5 mov %esp,%ebp
  257. 7c1c: 66 83 ec 10 sub $0x10,%esp
  258. ....
  259. This is because those instructions are modified by the prefix `0x66`, which makes them behave like 32-bit.
  260. === No linker script
  261. Print `hello world` without using an explicit linker script:
  262. ....
  263. make -C no-linker-script run
  264. ....
  265. Sources:
  266. * link:no-linker-script/Makefile[]
  267. * link:no-linker-script/main.S[]
  268. Uses the default host `ld` script, not an explicit one set with `-T`. Uses:
  269. * `-tText`
  270. * `.org` inside each assembly file
  271. * `_start` must be present to avoid a warning, since the default linker script expects it
  272. This is a hack, it can be more convenient for quick and dirty tests, but just don't use it.
  273. == BIOS
  274. The BIOS is one of the most well known firmwares in existence.
  275. A firmware is a software a software that:
  276. * runs before the OS / bootloader to do very low level setup
  277. * usually closed source, provided by the vendor, and interacts with undocumented hardware APIs
  278. * offers an API to the OS / bootloader, that allows you to do things like quick and dirty IO
  279. * undistinguishable from an OS, except that is it usually smaller
  280. BIOS is old, non-standardized, x86 omnipresent and limited.
  281. <<uefi>> is the shiny new overbloated thing.
  282. If you are making a serious OS, use it as little as possible.
  283. BIOS Can only be used in <<real-mode>>.
  284. BIOS functions are all accessed through the `int` instruction:
  285. ....
  286. mov <function-id>, %ah
  287. int <interrupt-id>
  288. ....
  289. Function arguments are stored in other registers.
  290. The interrupt IDs are traditionally in hex as:
  291. ....
  292. 10h
  293. ....
  294. which is the same as `0x10`.
  295. Each `interrupt-id` groups multiple functions with similar functions, e.g. `10h` groups functions with video related functionality.
  296. Bibliography:
  297. * https://en.wikipedia.org/wiki/BIOS
  298. * http://wiki.osdev.org/BIOS
  299. === BIOS documentation
  300. Does any official documentation or standardization exist?
  301. * https://en.wikipedia.org/wiki/BIOS_interrupt_call#Interrupt_table
  302. * http://www.ctyme.com/intr/int.htm Ralf Brown's Interrupt List. Everyone says that this is the ultimate unofficial compilation.
  303. * https://en.wikipedia.org/wiki/INT_10H good quick summary
  304. * http://www.scs.stanford.edu/nyu/04fa/lab/specsbbs101.pdf says little about interrupts, I don't understand it's scope.
  305. === BIOS examples
  306. Print a single `@` character:
  307. ....
  308. ./run bios_putc
  309. ....
  310. Source: link:bios_putc.S[]
  311. Print a newline:
  312. ....
  313. ./run bios_newline
  314. ....
  315. Source: link:bios_newline.S[]
  316. Outcome:
  317. ....
  318. hello
  319. world
  320. ....
  321. Carriage returns are needed just like in old days:
  322. ....
  323. ./run bios_carriage_return
  324. ....
  325. Source: link:bios_carriage_return.S[]
  326. Outcome:
  327. ....
  328. hello
  329. world
  330. ....
  331. Change the current cursor position:
  332. ....
  333. ./run bios_cursor_position
  334. ....
  335. Source: link:bios_cursor_position.S[]
  336. Outcome:
  337. ....
  338. cb
  339. ....
  340. ==== BIOS color
  341. Color codes: https://en.wikipedia.org/wiki/BIOS_color_attributes
  342. Write a character N times with given color:
  343. ....
  344. ./run bios_color
  345. ....
  346. Source: link:bios_color.S[]
  347. Outcome:
  348. ....
  349. bcd
  350. ....
  351. where:
  352. * `b` and `c` have red foreground, and green background
  353. * `d` has the default color (gray on black)
  354. Change the background color to red for the entire screen and print an `a` character:
  355. ....
  356. ./run bios_background
  357. ....
  358. Source: link:bios_background.S[]
  359. ==== BIOS scroll
  360. Scroll the screen:
  361. ....
  362. ./run bios_scroll
  363. ....
  364. Source: link:bios_scroll.S[]
  365. Outcome:
  366. ....
  367. a
  368. c
  369. GG
  370. d
  371. ....
  372. where `G` are empty green squares.
  373. How it works:
  374. Before scroll:
  375. ....
  376. a
  377. b
  378. c
  379. d
  380. ....
  381. We then choose to act on the rectangle with corners (1, 1) and (2, 2) given by `cx` and `dx`:
  382. ....
  383. a
  384. XX
  385. YY
  386. d
  387. ....
  388. and scroll that rectangle up by one line.
  389. `Y` is then filled with the fill color green
  390. ===== BIOS clear screen
  391. Subset of scroll:
  392. ....
  393. ./run bios_clear_screen
  394. ....
  395. Source: link:bios_clear_screen.S[]
  396. Outcome:
  397. ....
  398. b
  399. ....
  400. on red foreground, and the entire screen in green background, without any initial SeaBIOS messages.
  401. ==== BIOS draw pixel
  402. Make the pixel at position (1, 1) clear red color (0Ch) in <<video-mode-13h>>:
  403. ....
  404. ./run bios_pixel
  405. ....
  406. Source: link:bios_pixel.S[]
  407. You may have to look a bit hard to see it.
  408. Draw a line of such pixels:
  409. ....
  410. ./run bios_pixel_line
  411. ....
  412. Source: link:bios_pixel_line.S[]
  413. Advanced graphics!
  414. ==== BIOS keyboard
  415. Get one character from the user via the keyboard, increment it by one, and print it to the screen, then halt:
  416. ....
  417. ./run bios_keyboard
  418. ....
  419. Source: link:bios_keyboard.S[]
  420. Type a bunch of characters and see them appear on the screen:
  421. ....
  422. ./run bios_keyboard_loop
  423. ....
  424. Source: link:bios_keyboard_loop.S[]
  425. Do try `Ctrl-key` combinations.
  426. Bibliography: https://stackoverflow.com/questions/4113250/how-to-handle-keyboard-in-real-mode-through-bios-interrupts/32682518#32682518
  427. ==== BIOS disk load
  428. Load a stage 2 from disk with `int 13h` and run it:
  429. ....
  430. ./run bios_disk_load
  431. ....
  432. Source: link:bios_disk_load.S[]
  433. Outcome:
  434. ....
  435. a
  436. ....
  437. This character was printed from stage 2.
  438. Load two sectors instead of just one:
  439. ....
  440. ./run bios_disk_load2
  441. ....
  442. Source: link:bios_disk_load2.S[]
  443. Outcome:
  444. ....
  445. ab
  446. ....
  447. where `a` was printed from code on the first block, and `b` from code on the second block.
  448. This shows that each sector is 512 bytes long.
  449. GRUB 2.0 makes several calls to it under `grub-core/boot/i386/pc`.
  450. TODO: not working on Bochs: `BOUND_GdMa: fails bounds test`.
  451. But it does work on QEMU and <<thinkpad-t400>>.
  452. Bibliography:
  453. * https://en.wikipedia.org/wiki/INT_13H
  454. * http://wiki.osdev.org/ATA_in_x86_RealMode_%28BIOS%29
  455. * https://thiscouldbebetter.wordpress.com/2011/03/15/creating-a-bootable-program-in-assembly-language/
  456. * http://stackoverflow.com/questions/19381434/cannot-read-disk-sectors-in-assembly-language
  457. * http://stackoverflow.com/questions/15497842/read-a-sector-from-hard-drive-with-int-13h
  458. ==== BIOS detect memory
  459. TODO failed attempt at detecting how big our memory is with `int 15h`:
  460. ....
  461. ./run bios_detect_memory
  462. ....
  463. Source: link:bios_detect_memory.S[]
  464. Seems to output trash currently.
  465. This is important in particular so that you can start your stack there when you enter <<protected-mode>>, since the stack grows down.
  466. In 16-bit mode, it does not matter much, since most modern machines have all addressable memory there, but in 32-bit protected it does, as our emulator usually does not have all 4Gb. And of course, 64-bit RAM is currently larger than the total RAM in the world.
  467. `int 15` returns a list: each time you call it a new memory region is returned.
  468. The format is not too complicated, and documented at: http://wiki.osdev.org/Detecting_Memory_%28x86%29#Detecting_Upper_Memory
  469. * 8 bytes: base address of region.
  470. * 8 bytes: length of region.
  471. * 4 bytes: type or region. 1 for usable RAM.
  472. * 4 bytes: some ACPI stuff that no one uses?
  473. Bibliography: http://wiki.osdev.org/Detecting_Memory_%28x86%29
  474. ===== Low vs high memory
  475. TODO example.
  476. `int 15h` can detect low or high memory. How are they different?
  477. ==== BIOS sleep
  478. Count to infinity, sleep one second between each count:
  479. ....
  480. ./run bios_sleep
  481. ....
  482. Source: link:bios_sleep.S[]
  483. Polls time counter that BIOS keeps up to date at `0x046C` with frequency 18.2Hz eighteen times.
  484. Bibliography: https://stackoverflow.com/questions/9971405/how-to-display-a-number-on-the-screen-and-and-sleep-for-one-second-with-dos-x86/9973442#9973442
  485. ==== BIOS initial state
  486. Check the initial state the firmware leaves us by printing the contents of several registers:
  487. ....
  488. ./run bios_initial_state
  489. ....
  490. Source: link:bios_initial_state.S[]
  491. Outcome:
  492. ....
  493. ax = 00 00
  494. bx = 00 00
  495. cx = 00 00
  496. dx = 80 00
  497. cs = 00 00
  498. ds = 00 00
  499. es = 00 00
  500. fs = 00 00
  501. gs = 00 00
  502. ss = 00 00
  503. cr0 = 53 FF 00 F0
  504. ....
  505. `dx` seems to be like the only interesting regular register: the firmware stores the value of the current disk number to help with `int 15h` there. Thus it usually contains `0x80`.
  506. === dmidecode
  507. Get BIOS information. On host:
  508. ....
  509. sudo dmidecode
  510. ....
  511. Standardized by: https://en.wikipedia.org/wiki/Distributed_Management_Task_Force
  512. TODO: how is it obtained at the low level?
  513. Bibliography:
  514. * http://stackoverflow.com/questions/20604644/how-to-check-the-bios-version-or-name-in-linux-through-command-prompt
  515. * https://en.wikipedia.org/wiki/System_Management_BIOS SMBIOS
  516. === SeaBIOS
  517. http://www.seabios.org/SeaBIOS
  518. Open source x86 BIOS implementation.
  519. Default BIOS for QEMU and KVM.
  520. == No BIOS
  521. Here we will collect some examples that do stuff without using the BIOS!
  522. These tend to be less portable, not sure they will work on real hardware.
  523. Also they might need to rely on undocumented features.
  524. But they were verified in QEMU.
  525. If you are serious about this, study <<coreboot>>.
  526. === No BIOS hello world
  527. ....
  528. ./run no_bios_hello_world
  529. ....
  530. Source: link:no_bios_hello_world.S[]
  531. Outcome:
  532. ....
  533. hello world
  534. ....
  535. with red foreground and blue background shows on the top left of the cleared screen.
  536. This example uses the fact that BIOS maps video memory to address 0xB8000.
  537. We can then move 0xB800 to a segment register and use segment:offset addressing to access this memory.
  538. Then we can show characters by treating `0xB800:0000` as a `uint16_t` array, where low 8 bytes is the ASCII character, and the high 8 bytes is the color attribute of this character.
  539. == Modes of operation
  540. The x86 processor has a few modes, which have huge impact on how the processor works.
  541. Covered on the <<intel-manual>> Volume 3. Specially useful is the "Figure 2-3. Transitions Among the Processor’s Operating Modes" diagram.
  542. The modes are:
  543. * Real-address, usually known just as "real mode"
  544. * Protected
  545. * System management
  546. * IA-32e. Has two sub modes:
  547. ** Compatibility
  548. ** 64-bit
  549. * Virtual-8086 Mode
  550. Transition tables:
  551. ....
  552. (all modes)
  553. |
  554. | Reset
  555. |
  556. v
  557. +---------------------+
  558. | Real address (PE=0) |
  559. +---------------------+
  560. ^
  561. |
  562. | PE
  563. |
  564. v
  565. +------------------------+
  566. | Protected (PE=1, VM=0) |
  567. +------------------------+
  568. ^ ^
  569. | |
  570. | | VM
  571. | |
  572. v v
  573. +--------------+ +---------------------+
  574. | IA-32e | | Virtual-8086 (VM=1) |
  575. +--------------+ +---------------------+
  576. ....
  577. and:
  578. ....
  579. +------------------------+
  580. | System management mode |
  581. +------------------------+
  582. | ^
  583. | |
  584. | RSM | SMI#
  585. | |
  586. v |
  587. (All other modes)
  588. ....
  589. The IA-32e transition is trickier, but clearly described on the <<intel-manual>> Volume 3 - 9.8.5 "Initializing IA-32e Mode":
  590. ____
  591. Operating systems should follow this sequence to initialize IA-32e mode:
  592. 1. Starting from protected mode, disable paging by setting `CR0.PG = 0`. Use the `MOV CR0` instruction to disable paging (the instruction must be located in an identity-mapped page).
  593. 2. Enable physical-address extensions (PAE) by setting CR4.`PAE = 1`. Failure to enable PAE will result in a `#GP` fault when an attempt is made to initialize IA-32e mode.
  594. 3. Load `CR3` with the physical base address of the Level 4 page map table (PML4).
  595. 4. Enable IA-32e mode by setting `IA32_EFER.LME = 1`.
  596. 5. Enable paging by setting `CR0.PG = 1`. This causes the processor to set the `IA32_EFER.LMA` bit to 1. The `MOV CR0` instruction that enables paging and the following instructions must be located in an identity-mapped page (until such time that a branch to non-identity mapped pages can be effected).
  597. ____
  598. === Legacy modes
  599. The term defined in the <<intel-manual>> Volume 3 - CHAPTER 2 "SYSTEM ARCHITECTURE OVERVIEW":
  600. ____
  601. Real mode, protected mode, virtual 8086 mode, and system management mode. These are sometimes referred to as legacy modes.
  602. ____
  603. In other words: anything except IA-32e and System management mode.
  604. This further suggests that real, protected and virtual mode are not the main intended modes of operation.
  605. === Real mode
  606. http://wiki.osdev.org/Real_Mode
  607. The CPU starts in this mode after power up.
  608. All our <<bios>> examples are in real mode.
  609. It is possible to use 32-bit registers in this mode with the "Operand Size Override Prefix" `0x66`.
  610. TODO is it possible to access memory above 1M like this:
  611. ....
  612. mov $1, 0xF0000000
  613. mov $1, (%eax)
  614. ....
  615. http://stackoverflow.com/questions/6917503/is-it-possible-to-use-32-bits-registers-instructions-in-real-mode
  616. ==== Real mode segmentation
  617. ....
  618. ./run real_segmentation
  619. ....
  620. Source: link:real_segmentation.S[]
  621. Outcome:
  622. ....
  623. AAAAAA
  624. ....
  625. We access the character `A` with segments in 6 different ways:
  626. * `ds`, with explicit and implicit segment syntax
  627. * `es`, `fs`, `gs`, `ss`
  628. Segment registers modify the addresses that instructions actually use as:
  629. ....
  630. <segment> * 16 + <original-address>
  631. ....
  632. This implies that:
  633. * 20 bits of memory (1MB) instead of the 16 bits (256kB) that normally fits into registers. E.g., to address:
  634. +
  635. ....
  636. 0x84000
  637. ....
  638. +
  639. we can use:
  640. +
  641. ....
  642. 0x8000 (segment)
  643. 0x 4000 (address)
  644. -------
  645. 0x84000
  646. ....
  647. * most addresses can be encoded in multiple ways, e.g.:
  648. +
  649. ....
  650. 0x100
  651. ....
  652. +
  653. can be encoded as either of:
  654. +
  655. ** segment = `0x10`, address = `0`
  656. ** segment = `0`, address = `0x100`
  657. ** segment = `0x1`, address = `0xF0`
  658. `fs` and `gs` are general purpose: they are not affected implicitly by any instructions. All others will be further exemplified.
  659. ===== CS
  660. https://stackoverflow.com/questions/17777146/what-is-the-purpose-of-cs-and-ip-registers-in-intel-8086-assembly/33177253#33177253
  661. Affects the code address pointer:
  662. ....
  663. ./run cs
  664. ....
  665. Source: link:cs.S[]
  666. Outcome:
  667. ....
  668. 00
  669. 01
  670. 02
  671. ....
  672. `CS` is set with the `ljmp` instruction, and we use it to skip `.skip` zero gaps in the code.
  673. ===== SS
  674. ....
  675. ./run ss
  676. ....
  677. Source: link:ss.S[]
  678. Outcome:
  679. ....
  680. 0102
  681. ....
  682. The second byte is 16 bytes after the first, and is accessed with `SP = 1`.
  683. `SS` affects instructions that use `SP` such as `PUSH` and `POP`: those will actually use `16 * SS + SP` as the actual address.
  684. ===== ES
  685. TODO: this does seem to have special properties as used by string instructions.
  686. ===== Segment register encoding
  687. ....
  688. objdump -D -b binary -m i8086 segment_registers.img
  689. ....
  690. shows that non `ds` encodings are achieved through a prefix:
  691. ....
  692. 20: a0 63 7c mov 0x7c63,%al
  693. 34: 26 a0 63 7c mov %es:0x7c63,%al
  694. 40: 64 a0 63 7c mov %fs:0x7c63,%al
  695. 4c: 65 a0 63 7c mov %gs:0x7c63,%al
  696. 58: 36 a0 63 7c mov %ss:0x7c63,%al
  697. ....
  698. This makes `ds` the most efficient one for data access, and thus a good default.
  699. ==== Interrupts
  700. Create an interrupt handler and handle an interrupt:
  701. ....
  702. ./run interrupt
  703. ....
  704. Source: link:interrupt.S[]
  705. Outcome:
  706. ....
  707. ab
  708. ....
  709. It works like this:
  710. * print `a` an interrupt handler `0`
  711. * jump back to main code
  712. * print `b`
  713. TODO: is STI not needed because this interrupt is not maskable?
  714. Same with interrupt handler `1`:
  715. ....
  716. ./run interrupt1
  717. ....
  718. Source: link:interrupt1.S[]
  719. TODO understand: attempt to create an infinite loop that calls the interrupt from the handler:
  720. ....
  721. ./run interrupt_loop
  722. ....
  723. Source: link:interrupt_loop.S[]
  724. QEMU exits with:
  725. ....
  726. Trying to execute code outside RAM or ROM at 0x000a0000
  727. ....
  728. Handle a division by zero:
  729. ....
  730. ./run interrupt_zero_divide
  731. ....
  732. Source: link:interrupt_zero_divide.S[]
  733. TODO understand:
  734. * expected outcome: prints values from 0 to `0xFFFF` in an infinite loop.
  735. * actual outcome: stops at `0081`
  736. Apparently when there is an exception, `iret` jumps back to the line that threw the exception itself, not the one after, which leads to the loop:
  737. * https://stackoverflow.com/questions/33029457/what-to-do-in-interrupt-handler-for-divide-by-zero
  738. * https://stackoverflow.com/questions/9151429/os-development-how-to-avoid-an-infinite-loop-after-an-exception-routine
  739. But then why does it stop at `0081`? And if we set the initial value to `0x0090`, it just runs once.
  740. ===== int
  741. * long jumps to the CS : IP found in the corresponding interrupt vector.
  742. * pushes EFLAGS to let them be restored by iret?
  743. ===== iret
  744. Jumps back to the next instruction to be executed before the interrupt came in.
  745. Restores EFLAGS and other registers TODO which?
  746. Vs `jmp`: http://stackoverflow.com/questions/10462884/must-iret-be-used-when-returning-from-an-interrupt
  747. ===== Interrupt service routines
  748. Fancy name for the handler: http://wiki.osdev.org/Interrupt_Service_Routines
  749. ===== IVT
  750. Interrupt vector table: https://wiki.osdev.org/IVT
  751. The real mode in-memory table that stores the address for the handler for each interrupt.
  752. In <<protected-mode>>, the analogous structure is the <<idt>>.
  753. The base address is set in the interrupt descriptor table register (IDTR), which can be modified with the <<lidt>> instruction.
  754. The default address is `0x0`.
  755. The format of the table is:
  756. ....
  757. IDTR -> +-----------------------+
  758. 0 |Address (2 bytes) |
  759. 2 |Code segment (2 bytes) |
  760. +-----------------------+
  761. +-----------------------+
  762. 4 ----> |Address (2 bytes) |
  763. 6 |Code segment (2 bytes) |
  764. +-----------------------+
  765. +-----------------------+
  766. 8 ----> |Address (2 bytes) |
  767. A |Code segment (2 bytes) |
  768. +-----------------------+
  769. ... ...
  770. ....
  771. ====== lidt
  772. Set the value of the IDTR, and therefore set the base address of the <<ivt>>:
  773. ....
  774. ./run lidt
  775. ./run lidt2
  776. ./run lidt0
  777. ....
  778. Sources:
  779. * link:lidt.S[]
  780. * link:lidt2.S[]
  781. * link:lidt0.S[]
  782. TODO not working.
  783. Expected outcome:
  784. ....
  785. ab
  786. ....
  787. Actual outcome: infinite reboot loop.
  788. Actual outcome if we comment out the `PUTC`:
  789. * `lidt`: still infinite reboot loop
  790. * `lidt2` and `lidt0`: halt apparently
  791. I think I understand that `lidt` takes as input a memory address, and the memory at that address must contain:
  792. * 2 bytes: total size of the IVT in bytes
  793. * 4 bytes: base address of the IVT. Higher byte is ignored in real mode, since addresses are not 4 bytes long.
  794. === Protected mode
  795. Print `hello world` in protected mode:
  796. ....
  797. ./run protected_mode
  798. ....
  799. Source: link:protected_mode.S[]
  800. Major changes from real mode:
  801. * <<vga>> must be used for output since <<bios>> is not available in protected mode.
  802. * <<protected-mode-segmentation,segmentation>> takes effect immediately, so we have to set the <<gdt>> up
  803. * we have to encode instructions differently, thus a `.code32` is needed. 16-bit mode 32-bit instructions are encodable with a special prefix.
  804. Bibliography:
  805. * http://stackoverflow.com/questions/28645439/how-do-i-enter-32-bit-protected-mode-in-nasm-assembly Initially adapted from this.
  806. * http://wiki.osdev.org/Journey_To_The_Protected_Land
  807. * http://wiki.osdev.org/Protected_Mode
  808. * https://github.com/chrisdew/xv6/blob/master/bootasm.S
  809. * https://thiscouldbebetter.wordpress.com/2011/03/17/entering-protected-mode-from-assembly/ FASM based. Did not word on first try, but looks real clean.
  810. * http://skelix.net/skelixos/tutorial02_en.html
  811. * Linux kernel v4.12 `arch/x86/include/asm/segment.h`
  812. ==== Intel protected mode example
  813. Source: link:intel-protected/[]
  814. The <<intel-manual>> Volume 3 - 9.10 "INITIALIZATION AND MODE SWITCHING EXAMPLE" does contain an official example of how to go into protected mode.
  815. However:
  816. * the code is inside the PDF, which breaks all the formatting, so we have copied it here to this repo
  817. * TODO there is no known tool that can actually compile that syntax... although MASM should be close:
  818. ** http://computer-programming-forum.com/46-asm/6d9e8b7acea2d4cc.htm
  819. ** http://coding.derkeiler.com/Archive/Assembler/alt.lang.asm/2005-12/msg00028.html
  820. ** https://groups.google.com/forum/#!topic/comp.lang.asm.x86/9UZPQWwv-mQ 1994 comp.lang.asm.x86 topic
  821. How can those guys be in business? >:-)
  822. ==== Protected mode draw pixel
  823. TODO do it.
  824. Things get much more involved than in real mode: http://stackoverflow.com/questions/14419088/how-to-draw-a-pixel-on-the-screen-in-protected-mode-in-x86-assembly
  825. ==== Protected mode segmentation
  826. TODO: get working:
  827. ....
  828. ./run segmentation
  829. ....
  830. Source: link:segmentation.S[]
  831. Expected outcome:
  832. ....
  833. x
  834. a
  835. b
  836. ....
  837. Actual outcome:
  838. ....
  839. x
  840. a
  841. ....
  842. Example of the effect on a memory access of changing the segment base address.
  843. Without segment manipulation, the output would be just: TODO
  844. ===== Segmentation introduction
  845. First read the paging tutorial, and in particular: https://cirosantilli.com/x86-paging#segmentation to get a feel for the type of register and data structure manipulation required to configure the CPU, and how segmentation compares to paging.
  846. Segmentation modifies every memory access of a given segment by:
  847. * adding an offset to it
  848. * limiting how big the segment is
  849. If an access is made at an offset larger than allowed an exception happens, which is like an interrupt, and gets handled by a previously registered handler.
  850. Segmentation could be used to implement virtual memory by assigning one segment per program:
  851. ....
  852. +-----------+--------+--------------------------+
  853. | Program 1 | Unused | Program 2 |
  854. +-----------+--------+--------------------------+
  855. ^ ^ ^ ^
  856. | | | |
  857. Start1 End1 Start2 End2
  858. ....
  859. Besides address translation, the segmentation system also managed other features such as <<protection-rings>>. TODO: how are those done in 64-bit mode?
  860. In Linux 32-bit for example, only two segments are used at all times: one at ring 0 for the kernel, and one another at privilege 3 for all user processes.
  861. ===== Segment selector
  862. In protected mode, the segment registers `CS`, `DS`, `SS`, `ES`, `FS` and `GS` contain a data structure more complex than a simple address as in real mode, which contains a single number.
  863. This 2 byte data structure is called a _segment selector_:
  864. [options="header"]
  865. |===
  866. |Position (bits) |Size (bits) |Name |Description
  867. |0
  868. |2
  869. |Request Privilege Level (RPL)
  870. |Protection ring level, from 0 to 3.
  871. |2
  872. |1
  873. |Table Indicator (TI)
  874. a|
  875. * 0: global descriptor table
  876. * 1: local descriptor table
  877. |3
  878. |13
  879. |Index
  880. a|Index of the <<segment-descriptor>> to be used from the descriptor table.
  881. |===
  882. Like in real mode, this data structure is loaded on the registers with a regular `mov` mnemonic instruction.
  883. Bibliography: <<intel-manual>> Volume 3 - 3.4.5 "Segment Descriptors".
  884. ===== GDT
  885. Global descriptor table.
  886. An in-memory array of <<segment-descriptor>> data structures:
  887. The `Index` field of the <<segment-selector>> chooses which one of those segment descriptors is to be used.
  888. The base address is set with the `lgdt` instruction, which loads from memory a 6 byte structure:
  889. [options="header"]
  890. |===
  891. |Position (bytes) |Size (bytes) |Description
  892. |0
  893. |2
  894. |Number of entries in the table
  895. |2
  896. |4
  897. |Base address of the table
  898. |===
  899. Bibliography:
  900. * https://en.wikipedia.org/wiki/Global_Descriptor_Table
  901. * http://wiki.osdev.org/GDT
  902. ====== Local descriptor table
  903. TODO vs global?
  904. ====== Null segment selector
  905. <<intel-manual>> Volume 3 - 3.4.2 "Segment Selectors" says that we can't use the first entry of the GDT:
  906. ____
  907. The first entry of the GDT is not used by the processor. A segment selector that points to this entry of the GDT (that is, a segment selector with an index of 0 and the TI flag set to 0) is used as a “null segment selector.” The processor does not generate an exception when a segment register (other than the CS or SS registers) is loaded with a null selector. It does, however, generate an exception when a segment register holding a null selector is used to access memory. A null selector can be used to initialize unused segment registers. Loading the CS or SS register with a null segment selector causes a general-protection exception (#GP) to be generated.
  908. ____
  909. ===== Segment descriptor
  910. A data structure that is stored in the <<gdt>>.
  911. Clearly described on the <<intel-manual>> Volume 3 - 3.4.5 "Segment Descriptors" and in particular Figure 3-8 "Segment Descriptor".
  912. The Linux kernel v4.2 encodes it at: `arch/x86/include/asm/desc_defs.h` in `struct desc_struct`
  913. ===== Protection rings
  914. https://stackoverflow.com/questions/18717016/what-are-ring-0-and-ring-3-in-the-context-of-operating-systems/44483439#44483439
  915. TODO example. Jump to userspace, do something naughty, handler interrupt in kernel land.
  916. ==== IDT
  917. Interrupt descriptor table.
  918. Protected mode analogue to the <<ivt>>:
  919. ....
  920. ./run idt
  921. ....
  922. Source: link:idt.S[]
  923. Outcome:
  924. ....
  925. int 0 handled
  926. ....
  927. Handle interrupt 1 instead of 0:
  928. ....
  929. ./run idt1
  930. ....
  931. Source: link:idt1.S[]
  932. Outcome:
  933. ....
  934. int 1 handled
  935. ....
  936. Print `00000020\n` at `18.2 Hz` with the <<pit>>:
  937. ....
  938. ./run pit_protected
  939. ....
  940. Source: link:pit_protected.S[]
  941. Bibliography:
  942. * https://wiki.osdev.org/Interrupt_Descriptor_Table
  943. * https://en.wikipedia.org/wiki/Interrupt_descriptor_table
  944. * http://www.jamesmolloy.co.uk/tutorial_html/4.-The%20GDT%20and%20IDT.html
  945. The first 32 handlers are reserved by the processor and have predefined meanings, as specified in the <<intel-manual>> Volume 3 Table 3-3. "Intel 64 and IA-32 General Exceptions".
  946. In the Linux kernel, https://github.com/torvalds/linux/blob/v4.2/arch/x86/entry/entry_64.S sets them all up: each `idtentry divide_error` call sets up a new one.
  947. ===== IDT divide by zero
  948. Handle a division by zero:
  949. ....
  950. ./run idt_zero_divide
  951. ....
  952. Source: link:idt_zero_divide.S[]
  953. Outcome:
  954. ....
  955. division by zero handled
  956. ....
  957. Division by zero causes a Divide Error which Intel notes as `#DE`.
  958. It is then handled by IDT 0.
  959. DEs are not only for division by zero: they also happens on overflow. TODO example.
  960. ==== SMP
  961. link:https://en.wikipedia.org/wiki/Symmetric_multiprocessing[Symmetric multiprocessing].
  962. Verbose explanation: http://stackoverflow.com/questions/980999/what-does-multicore-assembly-language-look-like/33651438#33651438
  963. Start multiple processors and make them interact:
  964. ....
  965. ./run smp
  966. ....
  967. Source: link:smp.S[]
  968. Outcome:
  969. ....
  970. SMP started
  971. ....
  972. Implies that SMP worked because a spinlock was unlocked by the second processor.
  973. Try commenting out waking up the second processor and see it not get printed.
  974. ==== Paging
  975. Verbose beginner's tutorial: https://cirosantilli.com/x86-paging
  976. Change page tables and observe how that affects memory accesses:
  977. ....
  978. ./run paging
  979. ....
  980. Source: link:paging.S[]
  981. Outcome:
  982. ....
  983. 00001234
  984. 00005678
  985. ....
  986. Implies that paging worked because we printed and modified the same physical address with two different virtual addresses.
  987. Requires <<protected-mode>>.
  988. ===== Page fault
  989. Generate and handle a page fault:
  990. ....
  991. ./run page_fault
  992. ....
  993. Source: link:page_fault.S[]
  994. Outcome:
  995. ....
  996. Page fault handled. Error code:
  997. 00000002
  998. ....
  999. This is printed from a page fault handler that we setup an triggered by writing to an unmapped address.
  1000. === IA-32e mode
  1001. Wikipedia seems to call it long mode: https://en.wikipedia.org/wiki/Long_mode
  1002. Contains two sub-modes: <<64-bit-mode>> and <<compatibility-mode>>.
  1003. This controlled by the `CS.L` bit of the segment descriptor.
  1004. It appears that it is possible for user programs to modify that during execution from userland: http://stackoverflow.com/questions/12716419/can-you-enter-x64-32-bit-long-compatibility-sub-mode-outside-of-kernel-mode
  1005. TODO vs <<protected-mode>>.
  1006. === 64-bit mode
  1007. 64-bit is the major mode of operation, and enables the full 64 bit instructions.
  1008. There are currently no examples in this repo because I was lazy to make them.
  1009. As someone once brilliantly put it: https://twitter.com/garybernhardt/status/1106255947138125824
  1010. ____
  1011. Watching an x86-64 CPU boot is like watching an amoeba slowly evolve into a dog.
  1012. ____
  1013. The backward compatibility of x86 is mind boggling.
  1014. === Compatibility mode
  1015. Compatibility mode emulates IA-32 and allows to run 32 and 16 bit code.
  1016. But 64 bit Linux and Windows don't seem to allow 16 bit code anymore?
  1017. * http://stackoverflow.com/questions/27868394/switch-from-64-bit-long-mode-to-32-bit-compatibility-mode-on-x64
  1018. * https://stackoverflow.com/questions/7829058/how-to-run-16-bit-code-on-32-bit-linux
  1019. * https://superuser.com/questions/140953/why-cant-a-64-bit-os-run-a-16-bit-application
  1020. Compatibility vs protected: https://stackoverflow.com/questions/20848412/modes-of-intel-64-cpu
  1021. == in and out instructions
  1022. x86 has dedicated instructions for certain IO operations: `in` and `out`.
  1023. These instructions take an IO address which identifies which hardware they will communicate to.
  1024. The IO ports don't seem to be standardized, like everything else: http://stackoverflow.com/questions/14194798/is-there-a-specification-of-x86-i-o-port-assignment
  1025. The Linux kernel wraps those instructions with the `inb` and `outb` family of instructions:
  1026. ....
  1027. man inb
  1028. man outb
  1029. ....
  1030. === Memory mapped vs port mapped IO
  1031. Not all instruction sets have dedicated instructions such as `in` and `out` for IO.
  1032. In ARM for example, everything is done by writing to magic memory addresses.
  1033. The dedicated `in` and `out` approach is called "port mapped IO", and the approach of the magic addresses "memory mapp"
  1034. From an interface point of view, I feel that memory mapped is more elegant: port IO simply creates a second addresses space.
  1035. TODO: are there performance considerations when designing CPUs?
  1036. Bibliography: http://superuser.com/questions/703695/difference-between-port-mapped-and-memory-mapped-access
  1037. [[ps2-keyboard]]
  1038. === PS/2 keyboard
  1039. Whenever you press a key down or up, the keyboard hex scancode is printed to the screen:
  1040. ....
  1041. ./run ps2_keyboard
  1042. ....
  1043. Source: link:ps2_keyboard.S[]
  1044. Uses the PS/2 keyboard controller on `in 60h`: http://wiki.osdev.org/%228042%22_PS/2_Controller
  1045. The `in` always returns immediately with the last keyboard keycode: we then just poll for changes and print only the changes.
  1046. Scancode tables: TODO: official specs?
  1047. * https://en.wikipedia.org/wiki/Scancode#PC_compatibles
  1048. * http://flint.cs.yale.edu/cs422/doc/art-of-asm/pdf/APNDXC.PDF
  1049. TODO do this with the interrupt table instead of `in`. Failed attempt at: link:interrupt_keyboard.S[]
  1050. === PS/2 mouse
  1051. TODO create an example:
  1052. * http://wiki.osdev.org/Mouse_Input
  1053. * Random threads with source code, ah those OS devs:
  1054. ** https://forum.osdev.org/viewtopic.php?t=10247
  1055. ** https://forum.osdev.org/viewtopic.php?t=24277
  1056. * https://courses.engr.illinois.edu/ece390/books/labmanual/io-devices-mouse.html
  1057. I am so going to make a pixel drawing program with this.
  1058. === RTC
  1059. Real Time Clock: https://en.wikipedia.org/wiki/Real-time_clock
  1060. Get wall time with precision of seconds every second:
  1061. ....
  1062. ./run rtc
  1063. ....
  1064. Source: link:rtc.S[]
  1065. Sample outcome:
  1066. ....
  1067. 00 01 02 03 04 10
  1068. ....
  1069. which means:
  1070. ____
  1071. 3rd April 2010, 02 hours 01 minute and 00 seconds.
  1072. ____
  1073. Uses `out 70h` and `in 71h` to query the hardware.
  1074. This hardware must therefore use a separate battery to keep going when we turn off the computer or remove the laptop battery.
  1075. We can control the initial value in QEMU with the option:
  1076. ....
  1077. qemu-system-x86_64 -rtc base='2010-04-03T02:01:00'
  1078. ....
  1079. The RTC cannot give accuracy greater than seconds. For that, consider the <<pit>>, or the <<hpet>>.
  1080. Bibliography:
  1081. * http://wiki.osdev.org/RTC
  1082. * http://wiki.osdev.org/CMOS
  1083. * http://stackoverflow.com/questions/1465927/how-can-i-access-system-time-using-nasm
  1084. * https://github.com/torvalds/linux/blob/v4.2/arch/x86/kernel/rtc.c#L121
  1085. === PIT
  1086. Programmable Interval Timer: https://en.wikipedia.org/wiki/Programmable_interval_timer
  1087. Superseded by the <<hpet>>.
  1088. Print `a\n` with the minimal frequency possible of `0x1234DD / 0xFFFF = 18.2 Hz`:
  1089. ....
  1090. ./run pit
  1091. ....
  1092. Source: link:pit.S[]
  1093. Make the PIT generate a single interrupt instead of a frequency:
  1094. ....
  1095. ./run pit_once
  1096. ....
  1097. Source: link:pit_once.S[]
  1098. Outcome:
  1099. ....
  1100. a
  1101. ....
  1102. TODO I think this counts down from the value value in channel 0, and therefore allows to schedule a single event in the future.
  1103. The PIT can generate periodic interrupts (or <<pc-speaker,sound>>!) with a given frequency to `IRQ0`, which on real mode maps to interrupt 8 by default.
  1104. Major application: interrupt the running process to allow the OS to schedule processes.
  1105. The PIT 3 channels that can generate 3 independent signals
  1106. * channel 0 at port `40h`: generates interrupts
  1107. * channel 1 at port `41h`: not to be used for some reason
  1108. * channel 2 at port `42h`: linked to the speaker to generate sounds
  1109. Port `43h` is used to control signal properties except frequency, which goes in the channel ports, for the 3 channels.
  1110. Bibliography:
  1111. * http://wiki.osdev.org/PIT
  1112. * https://en.wikipedia.org/wiki/Intel_8253 that is the circuit ID for the PIT.
  1113. * http://kernelx.weebly.com/programmable-interval-timer.html
  1114. ==== PIT frequency
  1115. We don't control the frequency of the PIT directly, which is fixed at `0x1234DD`.
  1116. Instead, we control a frequency divisor. This is a classic type of discrete electronic circuit: https://en.wikipedia.org/wiki/Frequency_divider
  1117. The magic frequency comes from historical reasons to reuse television hardware according to link:https://wiki.osdev.org/Programmable_Interval_Timer[], which in turn is likely influenced by some physical properties of crystal oscillators.
  1118. The constant `1193181 == 0x1234DD` has 2 occurrences on Linux 4.16.
  1119. ==== HPET
  1120. Newer <<pit>>.
  1121. TODO example.
  1122. * https://en.wikipedia.org/wiki/High_Precision_Event_Timer
  1123. * https://wiki.osdev.org/HPET
  1124. ==== PC speaker
  1125. http://wiki.osdev.org/PC_Speaker
  1126. ....
  1127. ./run pc_speaker
  1128. ....
  1129. Source: link:pc_speaker.S[]
  1130. Outcome: produces a foul noisy noise using the PC speaker hardware on `out 61h`
  1131. QEMU only plays the sound if we give it the option:
  1132. ....
  1133. -soundhw pcspk
  1134. ....
  1135. The beep just uses the <<pit>> Channel 2 to generate the frequency.
  1136. Extracted from: https://github.com/torvalds/linux/blob/v4.2/arch/x86/realmode/rm/wakemain.c#L38 The kernel has a Morse code encoder using it!
  1137. Bibliography:
  1138. * https://courses.engr.illinois.edu/ece390/books/labmanual/io-devices-speaker.html
  1139. * http://fly.srk.fer.hr/GDM/articles/sndmus/speaker1.html
  1140. == Video mode
  1141. There are several video modes.
  1142. Modes determine what interrupt functions can be used.
  1143. There are 2 main types of modes:
  1144. * text, where we operate character-wise
  1145. * video, operate byte-wise
  1146. Modes can be set with `int 0x10` and `AH = 0x00`, and get with `AH = 0x0F`
  1147. The most common modes seem to be:
  1148. * 0x01: 40x25 Text, 16 colors, 8 pages
  1149. * 0x03: 80x25 Text, 16 colors, 8 pages
  1150. * 0x13: 320x200 Graphics, 256 colors, 1 page
  1151. You can add 128 to the modes to prevent them from clearing the screen.
  1152. Taken from: https://courses.engr.illinois.edu/ece390/books/labmanual/graphics-int10h.html
  1153. A larger list: http://www.columbia.edu/~em36/wpdos/videomodes.txt
  1154. See also: http://wiki.osdev.org/How_do_I_set_a_graphics_mode
  1155. === Video mode 13h
  1156. https://en.wikipedia.org/wiki/Mode_13h
  1157. Example at: <<bios-draw-pixel>>
  1158. Video Mode `13h` has: 320 x 200 Graphics, 256 colors, 1 page.
  1159. The color encoding is just an arbitrary palette that fits 1 byte, it is not split colors like R R R G G G B B or anything mentioned at: https://en.wikipedia.org/wiki/8-bit_color. Related: http://stackoverflow.com/questions/14233437/convert-normal-256-color-to-mode-13h-version-color
  1160. === VGA
  1161. * https://en.wikipedia.org/wiki/Video_Graphics_Array
  1162. * https://en.wikipedia.org/wiki/VGA-compatible_text_mode
  1163. TODO: what is it exactly?
  1164. BIOS cannot be used when we move into <<protected-mode>>, but we can use the VGA interface to get output out of our programs.
  1165. Have a look at the macros prefixed with `VGA_` inside link:common.h[].
  1166. == Power
  1167. === Reboot
  1168. Infinite reboot loop on emulator!
  1169. ....
  1170. ./run reboot
  1171. ....
  1172. Source: link:reboot.S[]
  1173. TODO why does it work?
  1174. Bibliography: http://stackoverflow.com/questions/32682152/how-to-reboot-in-x86-assembly-from-16-bit-real-mode
  1175. === APM
  1176. Turn on and immediately shutdown the system closing QEMU:
  1177. ....
  1178. ./run apm_shutdown
  1179. ....
  1180. Source: link:apm_shutdown.S[]
  1181. Fancier version copied from http://wiki.osdev.org/APM (TODO why is that better):
  1182. ....
  1183. ./run apm_shutdown2
  1184. ....
  1185. Source: link:apm_shutdown2.S[]
  1186. Older than <<acpi>> and simpler.
  1187. By Microsoft in 1995. Spec seems to be in RTF format...
  1188. Can't find the URL. A Google cache: https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CB0QFjAAahUKEwj7qpLN_4XIAhWCVxoKHa_nAxY&url=http%3A%2F%2Fdownload.microsoft.com%2Fdownload%2F1%2F6%2F1%2F161ba512-40e2-4cc9-843a-923143f3456c%2FAPMV12.rtf&usg=AFQjCNHoCx8gHv-w08Dn_Aoy6Q3K3DLWRg&sig2=D_66xvI7Y2n1cvyB8d2Mmg
  1189. Bibliography:
  1190. * https://en.wikipedia.org/wiki/Advanced_Power_Management
  1191. * http://wiki.osdev.org/APM
  1192. * http://wiki.osdev.org/Shutdown
  1193. * http://stackoverflow.com/questions/21463908/x86-instructions-to-power-off-computer-in-real-mode
  1194. * http://stackoverflow.com/questions/678458/shutdown-the-computer-using-assembly
  1195. * http://stackoverflow.com/questions/3145569/how-to-power-down-the-computer-from-a-freestanding-environment
  1196. === ACPI
  1197. TODO example
  1198. ACPI https://en.wikipedia.org/wiki/Advanced_Configuration_and_Power_Interface
  1199. Newer and better.
  1200. Now managed by the same group that manages UEFI.
  1201. Spec:
  1202. * current: http://uefi.org/specifications
  1203. * old: http://www.uefi.org/acpi/specs
  1204. == UEFI
  1205. https://en.wikipedia.org/wiki/Unified_Extensible_Firmware_Interface
  1206. Successor for <<bios>>.
  1207. All <<test-hardware,laptops I tested BIOS with>> had UEFI, so UEFI must have a BIOS emulation mode for backwards compatibility: https://www.howtogeek.com/56958/htg-explains-how-uefi-will-replace-the-bios/
  1208. Made by Intel, mostly MIT open source, which likely implies that vendors will hack away closed source versions.
  1209. link:https://mjg59.dreamwidth.org/10014.html[Matthew Garrett says] it is huge: larger than Linux without drivers.
  1210. Since it is huge, it inevitably contains bugs. Garret says that Intel sometimes does not feel like updating the firmware with bugfixes.
  1211. UEFI offers a large API comparable to what most people would call an operating system:
  1212. * https://software.intel.com/en-us/articles/uefi-application mentions a POSIX C library port
  1213. * https://lwn.net/Articles/641244/ mentions a Python interpreter port!
  1214. ARM is considering an implementation https://wiki.linaro.org/ARM/UEFI
  1215. === UEFI example
  1216. ....
  1217. make -C uefi run
  1218. ....
  1219. TODO get a hello world program working:
  1220. * http://www.rodsbooks.com/efi-programming/hello.html Best source so far: allowed me to compile the hello world! TODO: how to run it now on QEMU and real hardware?
  1221. * https://fedoraproject.org/wiki/Using_UEFI_with_QEMU
  1222. * https://wiki.ubuntu.com/UEFI/OVMF
  1223. * https://github.com/tqh/efi-example
  1224. Running without image gives the UEFI shell, and a Linux kernel image booted fine with it: link:http://unix.stackexchange.com/a/228053/32558[], so we just need to generate the image.
  1225. The blob `uefi/ovmf.fd` IA32 r15214 was downloaded from: https://sourceforge.net/projects/edk2/files/OVMF/OVMF-IA32-r15214.zip/download TODO: automate building it from source instead, get rid of the blob, and force push it away from history. Working build setup sketch: https://github.com/cirosantilli/linux-cheat/blob/b1c3740519eff18a7707de981ee3afea2051ba10/ovmf.sh
  1226. It seems that they have moved to GitHub at last: https://github.com/tianocore/tianocore.github.io/wiki/How-to-build-OVMF/e372aa54750838a7165b08bb02b105148e2c4190
  1227. === UEFI Bibliography
  1228. * https://www.youtube.com/watch?v=V2aq5M3Q76U hardcore kernel dev Matthew Garrett saying how bad UEFI is
  1229. * https://wiki.archlinux.org/index.php/Unified_Extensible_Firmware_Interface
  1230. * http://wiki.osdev.org/UEFI
  1231. == Coreboot
  1232. TODO minimal examples.
  1233. https://en.wikipedia.org/wiki/Coreboot
  1234. https://www.coreboot.org
  1235. Open source hippie freedom loving cross platform firmware that attempts to replace BIOS and UEFI for the greater good of mankind.
  1236. == GRUB
  1237. link:grub/README.adoc[] TODO cleanup and exemplify everything in that file. Some hosty stuff needs to go out maybe.
  1238. === GRUB chainloader
  1239. ....
  1240. make -C grub/chainloader run
  1241. ....
  1242. Outcome: you are left in an interactive GRUB menu with two choices:
  1243. * `hello-world`: go into a hello world OS
  1244. * `self +1`: reload ourselves, and almost immediately reload GRUB and fall on the same menu as before
  1245. This example illustrates the `chainloader` GRUB command, which just loads a boot sector and runs it: https://www.gnu.org/software/grub/manual/grub/html_node/chainloader.html
  1246. This is what you need to boot systems like Windows which GRUB does not know anything about: just point to their partition and let them do the job.
  1247. Both of the menu options are implemented with `chainloader`:
  1248. * `hello-world`:
  1249. +
  1250. Loads a given image file within the partition.
  1251. +
  1252. After build, `grub-mkrescue` creates a few filesystems, and `grub/chainloader/iso/boot/main.img` is placed inside one of those filesystems.
  1253. +
  1254. This illustrates GRUB's awesome ability to understand certain filesystem formats, and fetch files from them, thus allowing us to pick between multiple operating systems with a single filesystem.
  1255. +
  1256. It is educational to open up the generated `grub/chainloader/main.img` with the techniques described at https://askubuntu.com/questions/69363/mount-single-partition-from-image-of-entire-disk-device/673257#673257 to observe that the third partition of the image is a VFAT filesystem, and that it contains the `boot/main.img` image as a regular file.
  1257. * `self +1`: uses the syntax:
  1258. +
  1259. ....
  1260. chainloader +1
  1261. ....
  1262. +
  1263. which reloads the first sector of the current partition, and therefor ourselves.
  1264. TODO: why does it fail for hybrid ISO images? http://superuser.com/questions/154134/grub-how-to-boot-into-iso-partition#comment1337357_154271
  1265. === GRUB linux
  1266. TODO get working.
  1267. OK, let's have some fun and do the real thing!
  1268. ....
  1269. make -C grub/linux run
  1270. ....
  1271. Expected outcome: GRUB menu with a single `Buildroot` entry. When you select it, a tiny pre-built Linux image boots from: https://github.com/cirosantilli/linux-kernel-module-cheat
  1272. Actual outcome: after selecting the entry, nothing shows on the screen. Even if we fix this, we will then also need to provide a rootfs somehow: the `initrd` GRUB command would be a simple method, that repo can also generate initrd images: https://github.com/cirosantilli/linux-kernel-module-cheat/tree/c06476bfc821659a4731d49e808f45e8c509c5e1#initrd Maybe have look under Buildroot `boot/grub2` and copy what they are doing there.
  1273. The GRUB command is of form:
  1274. ....
  1275. linux /boot/bzImage root=/dev/sda1 console=tty1
  1276. ....
  1277. so we see that the kernel boot parameters are passed right there, for example try to change the value of the `printk.time` parameter:
  1278. ....
  1279. printk.time=y
  1280. ....
  1281. and see how the dmesg times not get printed anymore.
  1282. == Multiboot
  1283. https://en.wikipedia.org/wiki/Multiboot_Specification
  1284. Standard created by GRUB for booting OSes.
  1285. Multiboot files are an extension of ELF files with a special header.
  1286. Advantages: GRUB does housekeeping magic for you:
  1287. * you can store the OS as a regular file inside a filesystem
  1288. * your program starts in 32-bit mode already, not 16 bit real mode
  1289. * it gets the available memory ranges for you
  1290. Disadvantages:
  1291. * more boilerplate
  1292. GRUB leaves the application into a well defined starting state.
  1293. It seems that Linux does not implement Multiboot natively, but GRUB supports it as an exception: http://stackoverflow.com/questions/17909429/booting-a-non-multiboot-kernel-with-grub2
  1294. === Multiboot hello world
  1295. QEMU supports multiboot natively https://stackoverflow.com/questions/25469396/how-to-use-qemu-properly-with-multi-boot-headers/32550281#32550281:
  1296. ....
  1297. make -C multiboot/hello-world run
  1298. ....
  1299. which actually runs:
  1300. ....
  1301. qemu-system-i386 -kernel 'main.elf'
  1302. ....
  1303. where `main.elf` is the multiboot file we generated.
  1304. Outcome:
  1305. ....
  1306. hello world
  1307. ....
  1308. Or you can use `grub-mkrescue` to make a multiboot file into a bootable ISO or disk:
  1309. ....
  1310. qemu-system-x86_64 -drive file=main.img,format=raw
  1311. ....
  1312. The `main.img` file can also be burned to a USB and run on real hardware.
  1313. Example originally minimized from https://github.com/programble/bare-metal-tetris
  1314. This example illustrates the `multiboot` GRUB command: https://www.gnu.org/software/grub/manual/grub/html_node/multiboot.html
  1315. === osdev multiboot hello world
  1316. We also track here the code from: link:http://wiki.osdev.org/Bare_Bones[]:
  1317. ....
  1318. make -C multiboot/osdev run
  1319. ....
  1320. Outcome:
  1321. ....
  1322. hello world
  1323. ....
  1324. This is interesting as it uses C as much as possible with some GAS where needed.
  1325. This should serve as a decent basis for starting a pet OS. But please don't, there are enough out there already :-)
  1326. == Tests
  1327. === Unit tests
  1328. Tests for utilities defined in this repo, as opposed to x86 or external firmware concepts.
  1329. TODO: implement the function and enable this test: link:test_vga_print_bytes.S[]
  1330. ==== PRINT_BYTES
  1331. Print several bytes in human readable form:
  1332. ....
  1333. ./run test_print_bytes
  1334. ....
  1335. Source: link:test_print_bytes.S[]
  1336. Outcome:
  1337. ....
  1338. 40 41 42 43 44 45 46 47
  1339. 48 49 4A 4B 4C 4D 4E 4F
  1340. 50
  1341. ....
  1342. ==== PIT_SLEEP_TICKS
  1343. Print `a\n` with frequency 2Hz:
  1344. ....
  1345. ./run test_pit_sleep_ticks
  1346. ....
  1347. Source: link:test_pit_sleep_ticks.S[]
  1348. Same but in protected mode:
  1349. ....
  1350. ./run test_pit_sleep_protected
  1351. ....
  1352. Source: link:test_pit_sleep_protected.S[]
  1353. === Test hardware
  1354. ==== ThinkPad T400
  1355. Most of this repo was originally tested on a link:https://www.cnet.com/products/lenovo-thinkpad-t400/specs/[Lenovo ThinkPad T400].
  1356. Unfortunately it broke and I threw it away, and I didn't write down the exact specs before doing so, notably the bootloader version.
  1357. ==== ThinkPad T430
  1358. Then, when I moved to a new ThinkPad, I tested some of the examples on the link:https://www.cnet.com/products/lenovo-thinkpad-t400/specs/[Lenovo ThinkPad T430] I originally used to write this :-)
  1359. Firmware: UEFI BIOS 1.16.
  1360. == About
  1361. === System vs userland
  1362. This repository covers only things that can only be done from ring 0 (system) and not ring 3 (userland).
  1363. Ring 3 is covered at: https://github.com/cirosantilli/x86-assembly-cheat
  1364. An overview of rings 0 and 3 can be found at: https://stackoverflow.com/questions/18717016/what-are-ring-0-and-ring-3-in-the-context-of-operating-systems/44483439#44483439
  1365. === One minimal concept per OS
  1366. There are a few tutorials that explain how to make an operating system and give examples of increasing complexity with more and more functionality added: <<progressive-tutorials>>.
  1367. This is not one of them.
  1368. The goal of this repository is to use the minimal setup possible to be able to observe _a single_ low-level programming concept for each minimal operating system we create.
  1369. This is not meant provide a template from which you can write a real OS, but instead to illustrate how those low-level concepts work in isolation, so that you can use that knowledge to implement operating systems or drivers.
  1370. Minimal examples are useful because it is easier to observe the requirements for a given concept to be observable.
  1371. Another advantage is that it is easier to DRY up minimal examples with macros or functions, which is much harder on progressive OS template tutorials, which tend to repeat big chunks of code between the examples.
  1372. === To C or not to C
  1373. Using C or not is a hard choice.
  1374. It does make it much easier to express higher level ideas, and gives portability.
  1375. However, it increases the complexity that one has to understand a bit, so I decided to stay away from it when I wrote this tutorial.
  1376. But I have since change my mind, and if I ever touch this again seriously, I would rewrite it in C based on <<c-hello-world>> and Newlib: https://electronics.stackexchange.com/questions/223929/c-standard-libraries-on-bare-metal/400077#400077
  1377. If this is done, we this repo should then be merged into: https://github.com/cirosantilli/linux-kernel-module-cheat/tree/87e846fc1f9c57840e143513ebd69c638bd37aa8#baremetal-setup together with the ARM Newlib baremetal setups present there.
  1378. === Serial UART
  1379. What the heck is a serial in the real world: https://unix.stackexchange.com/questions/307390/what-is-the-difference-between-ttys0-ttyusb0-and-ttyama0-in-linux/367882#367882
  1380. Currently all text output is done the display, and that was a newbie design choice from before I knew the serial existed. The serial is just generally more minimal and elegant than the display, and should have been used instead.
  1381. ....
  1382. ./run bios_serial
  1383. cat bios_serial.tmp.serial
  1384. ....
  1385. On QEMU, we see the serial output on the host terminal:
  1386. ....
  1387. hello world
  1388. ....
  1389. and on Bochs we redirect it to a file:
  1390. ....
  1391. ./run bios_serial bochs
  1392. cat bios_serial.tmp.serial
  1393. ....
  1394. Source: link:bios_serial.S[]
  1395. TODO: failed attempt without BIOS:
  1396. ....
  1397. ./run serial
  1398. ....
  1399. Source: link:serial.S[]
  1400. Like every other of those old hardwares, it is impossible to find its documentation, must be rotting on some IBM mainframe that is not connected to the internet, so we go for:
  1401. * OSDev: https://wiki.osdev.org/Serial_Ports
  1402. * Samy likely just copied OSDev that for his: https://github.com/SamyPesse/How-to-Make-a-Computer-Operating-System/blob/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/kernel/modules/x86serial.cc#L38
  1403. Bibliography:
  1404. * https://stackoverflow.com/questions/22571379/intel-galileo-bare-metal-uart
  1405. * https://stackoverflow.com/questions/27594297/how-to-print-a-string-to-the-terminal-in-x86-64-assembly-nasm-without-syscall
  1406. This would open up:
  1407. * gem5 benchmarking and exploration, currently blocked on https://stackoverflow.com/questions/50364863/how-to-get-graphical-gui-output-and-user-touch-keyboard-mouse-input-in-a-ful/50364864#50364864
  1408. * the output stays persistently on the host terminal. So we can run QEMU without a GUI, immediatily shutdown the machine it at the end, and not have to close QEMU manually all the time.
  1409. * automated unit tests. Ha, like I'm gonna be that diligent!
  1410. * easily working on ARM in a more uniform way to prepare for the move in to https://github.com/cirosantilli/linux-kernel-module-cheat/tree/87e846fc1f9c57840e143513ebd69c638bd37aa8#baremetal-setup
  1411. === Macros vs functions
  1412. Using macros for now on link:common.h[] instead of functions because it simplifies the linker script.
  1413. But the downsides are severe:
  1414. * no symbols to help debugging. TODO: I think there are assembly constructs for that.
  1415. * impossible to step over method calls: you have to step into everything. TODO: `until`?
  1416. * larger output, supposing I can get linker gc for unused functions working, see `--gc-section`, which is for now uncertain.
  1417. +
  1418. If I can get this working, I'll definitely move to function calls.
  1419. +
  1420. The problem is that if I don't, every image will need a stage 2 loader. That is not too serious though, it could be added to the `BEGIN`.
  1421. +
  1422. It seems that `ld` can only remove sections, not individual symbols: http://stackoverflow.com/questions/6687630/c-c-gcc-ld-remove-unused-symbols With GCC we can use `-ffunction-sections -fdata-sections` to quickly generate a ton of sections, but I don't thing GAS supports that...
  1423. We should just rewrite the whole thing to use functions instead...
  1424. ==== Macro conventions
  1425. Every "function-like macro" in link:common.h[] must maintain the state of general purpose registers.
  1426. Flags are currently not maintained.
  1427. `%sp` cannot be used to pass most arguments.
  1428. We don't care about setting `%bp` properly at the moment.
  1429. === NASM
  1430. ....
  1431. cd nasm/
  1432. ./run bios_hello_world
  1433. ....
  1434. Source: link:nasm/bios_hello_world.asm[]
  1435. While NASM is a bit more convenient than GAS to write a boot sector, I think it is just not worth it.
  1436. When writing an OS in C, we are going to use GCC, which already uses GAS. So it's better to reduce the number of assemblers to one and stick to GAS only.
  1437. Right now, this directory is not very DRY since NASM is secondary to me, so it contains mostly some copy / paste examples.
  1438. On top of that, GAS also supports other architectures besides x86, so learning it is more useful in that sense.
  1439. === Linux is open source
  1440. Always try looking into the Linux kernel to find how those CPU capabilities are used in a "real" OS.
  1441. === Pre-requisites
  1442. OS dev is one of the most insanely hard programming tasks a person can undertake, and will push your knowledge of several domains to the limit.
  1443. Knowing the following will help a lot:
  1444. * userland x86 assembly: https://github.com/cirosantilli/assembly-cheat
  1445. * compilation, linking and ELF format basics
  1446. * GDB debugging
  1447. While it is possible to learn those topics as you go along, and it is almost certain that you will end up learning more about them, we will not explain them here in detail.
  1448. == Bibliography
  1449. === Intel manual
  1450. We are interested mostly in the "Intel Manual Volume 3 System Programming Guide", where system programming basically means "OS stuff" or "bare metal" as opposed to userland present in the other manuals.
  1451. This repository quotes by default the following revision: 325384-056US September 2015 https://web.archive.org/web/20151025081259/http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-system-programming-manual-325384.pdf
  1452. === Small educational projects
  1453. Fun, educational and useless:
  1454. * https://github.com/arjun024/mkeykernel, https://github.com/arjun024/mkernel
  1455. +
  1456. Worked, but bad build system: not `Makefile` or `.gitignore`.
  1457. The following did not work on my machine out of the box:
  1458. * https://github.com/apparentlymart/ToyOS
  1459. * https://github.com/rde1024/toyos
  1460. ==== Baremetal games
  1461. * https://github.com/programble/bare-metal-tetris tested on Ubuntu 14.04. Just works.
  1462. +
  1463. Has Multiboot and El Torito. Uses custom linker script.
  1464. +
  1465. Almost entirely in C `-nostdlib`, with very few inline `asm` commands, and a small assembly entry point. So a good tutorial in how to do the bridge.
  1466. * https://github.com/daniel-e/tetros Tetris that fits into bootloader.
  1467. * https://github.com/nanochess/fbird Flappy bird in the 512-byte boot sector.
  1468. * https://github.com/Overv/MineAssemble Minecraft
  1469. * https://github.com/tsoding/pinpog Pong / Breakout
  1470. * https://github.com/io12/bootmine Minesweeper game in a 512-byte boot sector.
  1471. === Tutorials
  1472. * https://farid.hajji.org/en/blog/46-hello-world-on-the-bare-metal
  1473. * https://arobenko.gitbooks.io/bare_metal_cpp/content/
  1474. ==== Educational NIXes
  1475. One complexity order above the minimal tutorials, one below actual kernels
  1476. * http://www.xinu.cs.purdue.edu/
  1477. * https://pdos.csail.mit.edu/6.828/2014/xv6.html
  1478. * https://en.wikipedia.org/wiki/MINIX, influenced Linux
  1479. * https://github.com/SerenityOS/serenity
  1480. ==== Educational non-NIXes
  1481. * https://github.com/intermezzOS/book
  1482. * https://github.com/flosse/rust-os-comparison
  1483. === Multi collaborator websites
  1484. * osdev.org is a major source for this.
  1485. ** http://wiki.osdev.org/C%2B%2B_Bare_Bones
  1486. ** http://wiki.osdev.org/Text_UI
  1487. ** http://wiki.osdev.org/GUI
  1488. * http://www.osdever.net/
  1489. * https://courses.engr.illinois.edu/ece390/books/labmanual/index.html Illinois course from 2004
  1490. === Progressive tutorials
  1491. ==== jamesmolloy
  1492. http://www.jamesmolloy.co.uk/tutorial_html/index.html
  1493. The classic tutorial. Highly recommended.
  1494. Multiboot based kernels of increasing complexity, one example builds on the last one. Non DRY as a result.
  1495. Cleaned up source code: https://github.com/cirosantilli/jamesmolloy-kernel-development-tutorials
  1496. Well known bugs: http://wiki.osdev.org/James_Molloy's_Tutorial_Known_Bugs That's what happens when you don't use GitHub.
  1497. Good tutorials, author seems to master the subject.
  1498. But he could learn more about version control and build automation: source code inside ugly tar.gz with output files.
  1499. ==== cfenollosa/os-tutorial
  1500. https://github.com/cfenollosa/os-tutorial
  1501. Ubuntu 18.04 usage: apply this patch https://github.com/cfenollosa/os-tutorial/pull/100 and then:
  1502. ....
  1503. cd 23-fixes
  1504. make run
  1505. ....
  1506. Starts with raw assembly + inludes, moves to C midway.
  1507. Raw stage-2 loader. No task scheduling yet, but the feature is... "scheduled" ;-)
  1508. Explains how to use the QEMU GDB stub and automates it on makefile, kudos.
  1509. Reviewed at: 7aff64740e1e3fba9a64c30c5cead0f88514eb62
  1510. ==== SamyPesse/How-to-Make-a-Computer-Operating-System
  1511. https://github.com/SamyPesse/How-to-Make-a-Computer-Operating-System
  1512. Has one big source tree that goes up to multitasking and a stdlib. Kernel written C++ and stdlib in C. TODO check: 64-bit, ring 0 vs ring 3? `git grep rax` has no hits, so I'm guessing no 64-bit.
  1513. Build failed on Ubunbu 18.04 with: https://github.com/SamyPesse/How-to-Make-a-Computer-Operating-System/issues/127 and I didn't bother to investigate.
  1514. Does have a `lucid32` Vagrant file for the host, but lazy to try it out.
  1515. Reviewed at: eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687
  1516. ==== Other progressive tutorials
  1517. * https://sourceforge.net/p/oszur11/code/ci/master/tree/
  1518. +
  1519. GitHub mirror: https://github.com/cirosantilli/oszur11-operating-system-examples
  1520. +
  1521. Several examples of increasing complexity. Found at: http://stackoverflow.com/questions/7130726/writing-a-hello-world-kernel
  1522. +
  1523. Just works, but examples are non-minimal, lots of code duplication and blobs. There must be around 20 El Torito blobs in that repo.
  1524. +
  1525. Multiboot based.
  1526. * http://www.brokenthorn.com/Resources/OSDevIndex.html
  1527. * http://skelix.net/skelixos/index_en.html
  1528. +
  1529. Cleaned up version: https://github.com/cirosantilli/skelix-os
  1530. +
  1531. Not tested yet.
  1532. +
  1533. GAS based, no multiboot used.
  1534. * https://github.com/littleosbook/littleosbook
  1535. === Actually useful
  1536. These are not meant as learning resources but rather as useful programs:
  1537. * https://github.com/scanlime/metalkit A more automated / general bare metal compilation system. Untested, but looks promising.
  1538. * Python without an "OS": https://us.pycon.org/2015/schedule/presentation/378/
  1539. === ARM
  1540. A list of ARM bare metal resources can be found at: https://github.com/cirosantilli/arm-assembly-cheat/tree/117f5d7d3458c028275ce112725f2e36f594f13c#bare-metal
  1541. == LICENSE
  1542. Copyright Ciro Santilli https://cirosantilli.com
  1543. https://www.gnu.org/licenses/gpl-3.0.txt[GPL v3] for executable computer program usage.
  1544. https://creativecommons.org/licenses/by-sa/4.0/[CC BY-SA v4] for human consumption usage in learning material, e.g. `.md` files, source code comments, using source code excerpts in tutorials. Recommended attribution:
  1545. * Single file adaptations:
  1546. +
  1547. ....
  1548. Based on https://github.com/cirosantilli/x86-bare-metal-examples/blob/<commit-id>/path/to/file.md under CC BY-SA v4
  1549. ....
  1550. * Multi-file adaptations:
  1551. +
  1552. ....
  1553. Based on https://github.com/cirosantilli/x86-bare-metal-examples/tree/<commit-id> under CC BY-SA v4
  1554. ....
  1555. If you want to use this work under a different license, contact the copyright owner, and he might make a good price.