Malloc-ing Printf | 32 Bit OS Dev (in C)
Adding malloc to printf() to dynamically allocate a buffer for the write() syscall, so that terminal escape codes can be added to printf() calls and possibly improve performance using 1 overall write() call. Also replacing printing in the kernel with this new printf(), to simplify things and remove cursor variables.
OS Dev playlist:
https://www.youtube.com/playlist?list=PLT7NbkyNWaqajsw8Xh7SP9KJwjfpP8TNX
Source code for this video (includes upcoming changes/additions for argc/argv):
https://git.sr.ht/~queso_fuego/amateur_os_video_sources/tree/master/item/43_printf_terminal_argc_argv
Git Repos:
Sourcehut: https://git.sr.ht/~queso_fuego/quesos
Github mirror: https://github.com/queso-fuego/amateuros
Join the Community Discord: https://discord.gg/yKm4T89QFn
Contact:
email: fuegoqueso@gmail.com
twitter: @Queso_Fuego
twitch: https://www.twitch.tv/queso_fuego
Notes:
- Address 0x300000 being writable without error may be due to the initial identity mapping of the first 4MB of memory, including setting the read/write flag.
When the original kernel addresses up to 4MB are unmapped, unmap_page() clears the present flag, but does not clear the read/write flag. So the page is still writeable.
It may be good to also clear the read/write flag from unmap_page(), not sure yet.
- printf() is still a bit error-prone. It would be better to check if len is greater than max by pre-computing the new len before adding to write_buffer, and calling a helper function to increase the max and realloc write_buffer to a larger size when needed.
- Adding a realloc() function would also be nice, instead of using malloc & free to allocate and copy to a larger or smaller buffer.
Next video:
- Passing argc & argv to called programs, the example would be loading a file in the editor by passing it on the commandline, otherwise editing a new file by default
- Let me know if there's anything specific you'd like to see!
Outline:
0:00 Fix hex conversion from 'A'-'F' to 10-15
1:30 Add malloc-ed buffer to printf()
13:34 Set up kernel malloc variables
33:56 Simplify printing and other kernel things with new printf()
1:05:38 Add separate print_hex() function for printf()
1:13:26 Use printf() in print_filetable.h
1:22:33 Use printf() in print_registers.h
1:33:14 Fixed terminal codes for FG/BG colors
1:38:41 Next up & Outro
Questions about setup/software/etc.?
Check the FAQ: https://queso_fuego.srht.site/about.html
Updates may be on twitter, YouTube channel community tab, or the community discord, if I remember to update anything.
Music credits:
Downtown Glow by Ghostrifter & Devyzed
Creative Commons — Attribution-NoDerivs 3.0 Unported — CC BY-ND 3.0
Music promoted by https://www.chosic.com/free-music/all/
#osdev #printf #programming