Malloc & Free, Higher half kernel | 32 bit OS Dev (in C)

Channel:
Subscribers:
5,840
Published on ● Video Link: https://www.youtube.com/watch?v=pm2L0TxofQU



Duration: 2:02:45
1,257 views
49


Adding malloc() & free() C functions and system calls to dynamically allocate memory at runtime from a loaded program, and making the kernel run in higher half memory. Malloc uses the virtual memory manager to allocate and map in new pages as needed.

This is Part 2 of the virtual memory and paging video here: https://www.youtube.com/watch?v=QIGun_BGuDw

OS Dev playlist:
https://www.youtube.com/playlist?list=PLT7NbkyNWaqajsw8Xh7SP9KJwjfpP8TNX

Source code for this video:
https://git.sr.ht/~queso_fuego/amateur_os_video_sources/tree/master/item/42_vmem_paging_malloc_free_high_half_kernel

Git Repos for the OS:
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:
- I thought casting to void* in certain areas would lend 1-byte arithmetic to a pointer value. Apparently that isn't guaranteed,or is UB or compiler defined. I should use char* or uint8_t* instead. I'll refactor to do this.

- malloc_phys_address could probably be changed and put inside the malloc_init() loop for allocating initial pages. Then the other lines using malloc_phys_address can be removed as they're never really needed.

- I learned about ENTRY("entry point symbol") for linker scripts later, to set the entry point. If that works for the OS C files it can be an alternative to __attribute__ ((section ("entry_section"))).

- I'm looking into making printf() in the near future to supersede needing print_string/dec/hex/etc. functions; it could simplify printing for different typed values in several areas.

Next video ideas:
- A better filesystem using LBA & directories
- Editor updates: scrolling, line numbers, etc.
- Soundblaster 16 support
- Processes/multitasking
or other things...

- Let me know if there's anything specific you'd like to see!

Outline:
0:00 malloc/free C functions & system calls
16:05 malloc.h implementation
52:37 Clean up malloc-ed memory in kernel
57:43 malloc/free test program
1:16:01 Use malloc in editor for file buffers
1:32:02 3rdstage.c, move kernel to higher half memory
1:59:53 Outro/Coming up

Software used:
VMware Workstation Player: https://www.vmware.com/products/workstation-player/workstation-player-evaluation.html
freeBSD: https://www.freebsd.org/
qemu: https://www.qemu.org/
vim: https://www.vim.org/
clang: https://clang.llvm.org/
davinci resolve: https://www.blackmagicdesign.com/products/davinciresolve/
OBS Studio: https://obsproject.com/

Peripherals:
Camera: Sony ZV-1, Elgato Camlink 4k
Microphone: Shure SM7B, Cloudlifter CL-1, Focusrite Scarlett Solo
Keyboard: HHKB Pro Hybrid Type S, white/blank keycaps
Mouse: Logitech M590

Updates for things may be on twitter, the YouTube channel community tab, the community discord, or mailing lists, if I remember.

Music credits:
"Inspired" Kevin MacLeod (incompetech.com)
Licensed under Creative Commons: By Attribution 4.0 License http://creativecommons.org/licenses/by/4.0/

"90's sitcom theme played badly in Square Waves" by Queso Fuego

#osdev #malloc #higherhalf







Tags:
c programming
os dev
operating systems
low level
queso fuego
kernel malloc
malloc and free
higher half kernel
linked list