In the example, the user state program's KERNEL_VIRT_ADDR is the kernel module printing address p here is hardcoding (first load kernel module, then the printed address assigned to KERNEL_VIRT_ADDR),
Can be passed in other ways.
2.6 Kernel verification.
Kernel module-----------------------
#include #include #include #include module_license ("GPL");
Module_author ("Wheelz");
Module_description ("Mmap demo");
static unsigned long p = 0;
static int __init init (void) {//Allocating shared memory (one page) p = __get_free_pages (Gfp_kernel, 0);
Setpagereserved (Virt_to_page (p));
PRINTK ("<1> p = 0x%08x\n", p);
Writes a string strcpy (P, "Hello world!\n") in shared memory;
return 0;
} static void __exit fini (void) {clearpagereserved (Virt_to_page (P)); Free_pages (p, 0);} module_init (init);
Module_exit (fini); User-state program---------------------------------#include #include #include #include #include page_size (4*1024) #define Page_offset 0xc0000000 #define KERNEL_VIRT_ADDR0XC5E3C000 int main () {char *buf; int fd; unsigned long phy_addr; Fd=open (
"/dev/mem", O_RDWR);
if (fd = = 1) perror ("open");
Phy_addr=kernel_virt_addr-page_offset; Buf=mmap (0, Page_size, prot_read|
Prot_write, map_shared, FD, PHY_ADDR);
if (buf = = map_failed) perror ("Mmap");
Puts (BUF);//print contents of Shared memory Munmap (buf,page_size);
Close (FD); return 0; }