README.adoc 61 KB

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