Linux kernel State and user state shared memory __linux

Source: Internet
Author: User

Linux kernel State and user state shared memory

1. Mmap system call (function)
void* mmap (void * addr, size_t len, int prot, int flags, int fd, off_t offset)
Memory-mapped function mmap, which is responsible for mapping the contents of the file to the virtual memory space of the process, by reading and modifying the memory, to realize the reading and modification of the file, without the need to call the Read,write and other operations.

2, Mmap system call (Parameters)
1) Addr: Specifies the starting address of the mapping, usually set to NULL, specified by the system.
2 Length: File lengths mapped to memory.
3) Prot: The protection mode of the mapping area can be:
Prot_exec: Map area can be executed
Prot_read: Map area can be read
Prot_write: Map area can be written

4 Flags: The characteristics of the mapping area can be:
Map_shared: Data written to the mapping area is copied back to the file and is allowed to be shared by other processes that map the file.
Map_private: Writes to the mapping area result in a copy of the mapping area (copy-on-write), and modifications made to the zone are not written back to the original file.

5 FD: A file descriptor returned by Open that represents the file to map.
6 Offset: At the beginning of the file offsets, must be the paging size of the integer multiple, usually 0, to start mapping from the file header.

3, Unlock the map
int Munmap (void *start,size_t length)
Function: Cancels the mapped memory pointed to by the parameter start, and the parameter length indicates the amount of memory to be canceled.
Return value: 0 is returned successfully, otherwise return-1, the reason for the error is in errno.

Example analysis
Mmap system Call

4. Virtual Memory Area
Virtual memory area is a homogeneous interval in the virtual address space of a process, that is, a contiguous address range with the same characteristics. The memory image of a process consists of the following parts: program code, data, BSS
and stack areas, as well as memory-mapped areas.

The memory area of a process can be viewed by:/proc/pid/maps
08048000-0804f000 R-xp 00000000 08:01 573748/sbin/rpc.statd #text
0804f000-08050000 rw-p 00007000 08:01 573748/sbin/rpc.statd #data
08050000-08055000 Rwxp 00000000 00:00 0 #bss
040000000-40015000 R-xp 00000000 08:01 933965/lib/ld2.3.2.so #text
40015000-40016000 rw-p 00014000 08:01 933965/lib/ld-2.3.2.so #data

The fields for each row are: Start_end perm offset Major:minor inode
1 Start: Virtual address for this area
2 End: This zone ends the virtual address
3 Perm: Read, write and execute permissions; represents what the process is allowed to do in this area. The last character in this field is either p for private or s for sharing.
4 Offset: The starting address of the mapped part in the file
5) Major, Minor: primary and secondary equipment number
6 Inode: Index node

5, Vm_area_struct
The Linux kernel uses the structure vm_area_struct (<linux/mm_types.h>) to describe virtual memory regions, several of which are as follows:
1 unsigned long Vm_start virtual memory region start address
2 unsigned long vm_end virtual memory region end Address

3) unsigned long vm_flags the region's mark. such as: Vm_io and vm_reserved. Vm_io marks the VMA as a memory-mapped IO region, Vm_io prevents the system from including the zone in the process's storage
Storage (core dump), the VM_RESERVED flag memory area cannot be swapped out.

6, Mmap equipment operation
Mapping a device refers to associating a segment of the user's space with an address on the device's memory. When the program reads and writes the address of this user space, it is actually accessing the device.

What functionality is required for the Mmap device method.
The Mmap method is a member of a file_oprations structure that is invoked when a mmap system call is made. Before that, the kernel had done a lot of work. Mmap device methods need to do is to establish
The page table of the virtual address to the physical address.
Int (*mmap) (struct file *, struct vm_area_struct *)

Mmap How to complete the page table creation.
There are two methods:
1 Use Remap_pfn_range to create all page tables at once;
2 Use the Nopage VMA method to create one page table at a time.

The work of constructing the page table can be done by the Remap_pfn_range function, which is the following prototype:
int Remap_pfn_range (struct vm_area_struct *vma, unsigned long addr,unsigned long pfn, unsigned long size, pgprot_t prot)

VMA: Virtual memory Area pointer
VIRT_ADDR: The starting value of the virtual address

PFN: The physical page frame number where the physical address to map is located, and the physical address can be >>page_shift.
Size: The magnitude of the area to map.
The Prot:vma protection property.


int Memdev_mmap (struct file*filp, struct vm_area_struct *vma)
{
Vma->vm_flags |= Vm_io;
Vma->vm_flags |= vm_reserved;
if (Remap_pfn_range (VMA, Vma->vm_start,
Virt_to_phys (dev->data) >> Page_shift,
Size
Vma->vm_page_prot))
Return-eagain;
return 0;
}

dev->data is the shared data that is passed to the user state.

7, Mmap Equipment Method Example
1) memdev. Source code

#ifndef _memdev_h_
#define _memdev_h_

#ifndef Memdev_major
#define MEMDEV_MAJOR 0/* Preset MEM main device number * *
#endif

#ifndef Memdev_nr_devs
#define MEMDEV_NR_DEVS 2/* Equipment Number * *
#endif

#ifndef memdev_size
#define MEMDEV_SIZE 4096
#endif

/*MEM Equipment Description Structure Body * *
struct MEM_DEV
{
Char *data;
unsigned long size;
};

#endif/* _memdev_h_ * *

2) MEMDEV.C Source code

#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#include <linux/kernel.h>
#include "Memdev.h"

static int mem_major = Memdev_major;

Module_param (mem_major, int, s_irugo);

struct Mem_dev *mem_devp; /* Device Structure body pointer * *

struct Cdev Cdev;

/* File Open function/*
int Mem_open (struct inode *inode, struct file *filp)
{
struct Mem_dev *dev;

/* Get the secondary equipment number * *
int num = MINOR (Inode->i_rdev);

if (num >= memdev_nr_devs)
Return-enodev;
dev = &mem_devp[num];

/* Assign the device description structure pointer to the file private data pointer.
Filp->private_data = Dev;

return 0;
}

/* File Release function/*
int mem_release (struct inode *inode, struct file *filp)
{
return 0;
}
static int Memdev_mmap (struct file*filp, struct vm_area_struct *vma)
{
struct Mem_dev *dev = filp->private_data; /* Get the device structure body pointer/*

Vma->vm_flags |= Vm_io;
Vma->vm_flags |= vm_reserved;


if (Remap_pfn_range Vma,vma->vm_start,virt_to_phys (dev->data) >>page_shift, VMA->VM_END-VMA->VM _start, Vma->vm_page_prot))
Return-eagain;

return 0;
}

/* File operation structure Body * *
static const struct File_operations Mem_fops =
{
. Owner = This_module,
. open = Mem_open,
. Release = Mem_release,
. mmap = Memdev_mmap,
};

/* Device driver Module loading function * *
static int memdev_init (void)
{
int result;
int i;

dev_t Devno = Mkdev (mem_major, 0);

/* Static Application Equipment Number * *
if (mem_major)
result = Register_chrdev_region (Devno, 2, "Memdev");
else/* Dynamic Allocation device number * *
{
result = Alloc_chrdev_region (&devno, 0, 2, "Memdev");
Mem_major = Major (Devno);
}

if (Result < 0)
return result;

/* Initialize CDEV structure * *
Cdev_init (&cdev, &mem_fops);
Cdev.owner = This_module;
Cdev.ops = &mem_fops;

/* Register character device * *
Cdev_add (&cdev, Mkdev (mem_major, 0), Memdev_nr_devs);

/* Allocate memory for device description structure * *
MEM_DEVP = Kmalloc (Memdev_nr_devs * sizeof (struct mem_dev), gfp_kernel);
if (!MEM_DEVP)/* Request failed * *
{
result =-Enomem;
Goto Fail_malloc;
}
memset (MEM_DEVP, 0, sizeof (struct mem_dev));

/* Allocate memory for the device * *
for (i=0 i < Memdev_nr_devs; i++)
{
Mem_devp[i].size = memdev_size;
Mem_devp[i].data = Kmalloc (memdev_size, Gfp_kernel);
memset (mem_devp[i].data, 0, memdev_size);
}

return 0;

Fail_malloc:
Unregister_chrdev_region (Devno, 1);

return result;
}

/* Module Unload function * *
static void Memdev_exit (void)
{
Cdev_del (&cdev); /* Logout Device * *
Kfree (MEM_DEVP); /* release device structure inside the deposit * *
Unregister_chrdev_region (Mkdev (mem_major, 0), 2); /* Release Device number * *
}

Module_author ("Yinjiabin");
Module_license ("GPL");

Module_init (Memdev_init);
Module_exit (Memdev_exit);

3 Test Program source code

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

int main ()
{
int FD;
Char *start;
Char buf[100];
Char *buf;

* * Open File/*
FD = open ("/dev/memdev0", O_RDWR);

BUF = (char *) malloc (100);
memset (buf, 0, 100);
Start=mmap (null,100,prot_read| prot_write,map_shared,fd,0);

/* Read the data * *
strcpy (Buf,start);
Sleep (1);
printf ("Buf 1 =%s\n", buf);

/* Write Data * *
strcpy (Start, "Buf is not null!");

memset (buf, 0, 100);
strcpy (Buf,start);
Sleep (1);
printf ("Buf 2 =%s\n", buf);


Munmap (start,100); /* Unlock Map * *
Free (BUF);
Close (FD);
return 0;
}

int Munmap (void * addr, size_t len)
The call unlocks a mapping relationship in the process address space, the address that is returned when the mmap () is invoked, and Len is the size of the map area. addr When the mapping relationship is lifted, access to the original mapped address will cause a segment error to occur.

int Msync (void * addr, size_t len, int flags)
In general, changes to shared content in a mapped space are not directly written back to the disk file, and are often performed after the call to Munmap (). You can implement the contents of the file on the disk consistent with the content of the shared memory area by calling Msync ().

There is a place to check a lot of information, there is no detailed explanation, after the success of the mmap, close file descriptor closed (FD). The shared memory area still exists, but the data is still protected.

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.