Let's now get hands on with the low-level page allocator and free APIs that we've learned about so far. In this section, we will show relevant code snippets, followed by an explanation where warranted, from our demo kernel module (ch8/lowlevel_mem/lowlevel_mem.c).
In the primary worker routine, bsa_alloc(), of our small LKM, we highlighted (in bold font) the code comments that show what we are trying to achieve. A few points to note:
- First, we do something very interesting: we use our small kernel "library" function klib_llkd.c:show_phy_pages() to literally show you how physical RAM page frames are identity mapped to kernel virtual pages in the kernel lowmem region! (The exact working of the show_phy_pages() routine is discussed very shortly):
// ch8/lowlevel_mem/lowlevel_mem.c
[...]
static int bsa_alloc(void)
{
int stat = -ENOMEM;
u64 numpg2alloc = 0;
const struct page *pg_ptr1;
/*...