Examples of user-state and kernel-shared memory usage

Source: Internet
Author: User

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.