Skip to content

Instantly share code, notes, and snippets.

@md-jamal
Created May 6, 2019 02:31
Show Gist options
  • Save md-jamal/3b9505c3baa1a490a9137a6b58a93a04 to your computer and use it in GitHub Desktop.
Save md-jamal/3b9505c3baa1a490a9137a6b58a93a04 to your computer and use it in GitHub Desktop.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/moduleparam.h>
#include <asm/io.h>
static void *virtual_addr;
static phys_addr_t physical_addr;
int size = 1024; /* allocate 1024 bytes */
static int alloc_init(void)
{
virtual_addr = kmalloc(size,GFP_ATOMIC);
if(!virtual_addr) {
/* handle error */
pr_err("memory allocation failed\n");
return -ENOMEM;
} else {
pr_info("Memory allocated successfully Virtual address:%p\n", virtual_addr);
physical_addr = virt_to_phys(virtual_addr);
pr_info("Physical address:%llx\n", physical_addr);
pr_info("Virtual address:%p\n", phys_to_virt(physical_addr));
}
return 0;
}
static void alloc_exit(void)
{
kfree(virtual_addr);
pr_info("Memory freed\n");
}
module_init(alloc_init);
module_exit(alloc_exit);
MODULE_LICENSE("GPL");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment