目录
一、内核内存管理框架
二、内核中常用动态分配
2.1 kmalloc
2.2 vmalloc
2.3 kmalloc & vmalloc 的比较
2.4 分配选择原则:
三、IO访问-------访问外设控制器的寄存器
四、led驱动
1. 读原理图
2. 查阅SOC芯片手册
3. 编写驱动
一、内核内存管理框架
内核将物理内存等分成N块4KB,称之为一页,每页都用一个struct page来表示,采用伙伴关系算法维护
内核地址空间划分图:
高端内存分为vmalloc区、持久映射区、固定映射区
3G~3G+896M:低端内存,直接映射 虚拟地址 = 3G + 物理地址
(低段内存虚拟地址和物理地址都是连续的。高端内存虚拟地址连续,物理地址不连续)
低端内存细分为:ZONE_DMA、ZONE_NORMAL
(DMA是用来和外设进行大量数据传输的器件,不借助CPU直接访问内存)
分配方式:
```c
1. kmalloc:小内存分配,slab算法
2. get_free_page:整页分配,2的n次方页,n最大为10
```
大于3G+896M:高端内存
细分为:vmalloc区、持久映射区、固定映射区
分配方式:vmalloc:虚拟地址连续,物理地址不连续
二、内核中常用动态分配
2.1 kmalloc
函数原型:
```c
void *kmalloc(size_t size, gfp_t flags);
```
kmalloc() 申请的内存位于直接映射区域,而且在物理上也是连续的,它们与真实的物理地址只有一个固定的偏移,因为存在较简单的转换关系,所以对申请的内存大小有限制,不能超过128KB。
较常用的 flags(分配内存的方法):
GFP_ATOMIC—— 分配内存的过程是一个原子过程,分配内存的过程不会被(高优先级进程或中断)打断;
GFP_KERNEL—— 正常分配内存;
GFP_DMA—— 给 DMA 控制器分配内存,需要使用该标志(DMA要求分配虚拟地址和物理地址连续)。
flags 的参考用法:
|– 进程上下文,可以睡眠 GFP_KERNEL
|– 异常上下文,不可以睡眠 GFP_ATOMIC
| |– 中断处理程序 GFP_ATOMIC
| |– 软中断 GFP_ATOMIC
| |– Tasklet GFP_ATOMIC
|– 用于DMA的内存,可以睡眠 GFP_DMA | GFP_KERNEL
|– 用于DMA的内存,不可以睡眠 GFP_DMA |GFP_ATOMIC
对应的内存释放函数为:
```c
void kfree(const void *objp);
```
```c
void *kzalloc(size_t size, gfp_t flags)
```
2.2 vmalloc
```c
void *vmalloc(unsigned long size);
```
vmalloc() 函数则会在虚拟内存空间给出一块连续的内存区,但这片连续的虚拟内存在物理内存中并不一定连续。由于 vmalloc() 没有保证申请到的是连续的物理内存,因此对申请的内存大小没有限制,如果需要申请较大的内存空间就需要用此函数了。
对应的内存释放函数为:
```c
void vfree(const void *addr);
``
注意:vmalloc() 和 vfree() 可以睡眠,因此不能从异常上下文调用。
2.3 kmalloc & vmalloc 的比较
kmalloc()、kzalloc()、vmalloc() 的共同特点是:
1. 用于申请内核空间的内存;
2. 内存以字节为单位进行分配;
3. 所分配的内存虚拟地址上连续;
kmalloc()、kzalloc()、vmalloc() 的区别是:
1. kzalloc 是强制清零的 kmalloc 操作;(以下描述不区分 kmalloc 和 kzalloc)
2. kmalloc 分配的内存大小有限制(128KB),而 vmalloc 没有限制;
3. kmalloc 可以保证分配的内存物理地址是连续的,但是 vmalloc 不能保证;
4. kmalloc 分配内存的过程可以是原子过程(使用 GFP_ATOMIC),而 vmalloc 分配内存时则可能产生阻塞;
5. kmalloc 分配内存的开销小,因此 kmalloc 比 vmalloc 要快;
一般情况下,内存只有在要被 DMA 访问的时候才需要物理上连续,但为了性能上的考虑,内核中一般使用 kmalloc(),而只有在需要获得大块内存时才使用 vmalloc()。
2.4 分配选择原则:
1. 小内存(< 128k)用kmalloc,大内存用vmalloc或get_free_page
2. 如果需要比较大的内存,并且要求使用效率较高时用get_free_page,否则用vmalloc
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include "mychar.h"
#define BUF_LEN 100
int major = 11;
int minor = 0;
int mychar_num = 1;
struct mychar_dev
{
struct cdev mydev;
char mydev_buf[BUF_LEN];
int curlen;
struct mutex lock;
/*Read wait queue and write wait queue*/
wait_queue_head_t rq;
wait_queue_head_t wq;
struct fasync_struct *pasync_obj;
};
struct mychar_dev *pgmydev = NULL;
int mychar_open(struct inode *pnode, struct file *pfile)
{
pfile->private_data = container_of(pnode->i_cdev, struct mychar_dev, mydev);
printk("mychar_open\n");
return 0;
}
int mychar_close(struct inode *pnode, struct file *pfile)
{
//printk("mychar_close\n");
/*C90 requires printk after the variable declaration*/
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
if(pmydev->pasync_obj != NULL)
fasync_helper(-1,pfile,0, &pmydev->pasync_obj);
return 0;
}
ssize_t mychar_read(struct file *pfile, char __user *puser, size_t count, loff_t *p_pos)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
int size = 0;
int ret = 0;
mutex_lock(&pmydev->lock);
if(pmydev->curlen <= 0)
{
if(pfile->f_flags & O_NONBLOCK)
{//non-blocking
mutex_unlock(&pmydev->lock);
printk("O_NONBLOCK No Data Read\n");
return -1;
}
else
{//blocking
mutex_unlock(&pmydev->lock);
ret = wait_event_interruptible(pmydev->rq,pmydev->curlen > 0);
if(ret)
{
printk("Wake up by signal\n");
return -ERESTARTSYS;
}
mutex_lock(&pmydev->lock);
}
}
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
ret = copy_to_user(puser, pmydev->mydev_buf, size);
if(ret)
{
mutex_unlock(&pmydev->lock);
printk("copy_to_user failed\n");
return -1;
}
memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
pmydev->curlen = pmydev->curlen - size;
mutex_unlock(&pmydev->lock);
/*Wake up interrupt*/
wake_up_interruptible(&pmydev->wq);
return size;
}
ssize_t mychar_write(struct file *pfile, const char __user *puser, size_t count, loff_t *p_pos)
{
int size = 0;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
mutex_lock(&pmydev->lock);
if(pmydev->curlen >= BUF_LEN)
{
if(pfile->f_flags & O_NONBLOCK)
{
mutex_unlock(&pmydev->lock);
printk("O_NONBLOCK can not write\n");
return -1;
}
else
{
mutex_unlock(&pmydev->lock);
ret = wait_event_interruptible(pmydev->wq,
pmydev->curlen < BUF_LEN);
if(ret)
{
printk("wake up by signal\n");
return -ERESTARTSYS;
}
mutex_lock(&pmydev->lock);
}
}
if(count > BUF_LEN - pmydev->curlen)
{
size = BUF_LEN - pmydev->curlen;
}
else
{
size = count;
}
ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
if(ret)
{
mutex_unlock(&pmydev->lock);
printk("copy_from_user failed\n");
return -1;
}
pmydev->curlen += size;
mutex_unlock(&pmydev->lock);
/*Wake up interrupt*/
wake_up_interruptible(&pmydev->rq);
if(pmydev->pasync_obj != NULL)
{
kill_fasync(&pmydev->pasync_obj, SIGIO, POLL_IN);
}
return size;
}
long mychar_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
int __user *pret = (int *)arg;
int maxlen = BUF_LEN;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
switch(cmd)
{
case MYCHAR_IOCTL_GET_MAXLEN:
ret = copy_to_user(pret, &maxlen, sizeof(int));
if(ret)
{
printk("copy_to_user MAXLEN failed\n");
return -1;
}
break;
case MYCHAR_IOCTL_GET_CURLEN:
mutex_lock(&pmydev->lock);
ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
mutex_unlock(&pmydev->lock);
if(ret)
{
printk("copy_to_user MAXLEN failed\n");
return -1;
}
break;
default:
printk("The cmd is unknow\n");
return -1;
}
return 0;
}
unsigned int mychar_poll(struct file *pfile, poll_table *ptb)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
unsigned int mask = 0;
/*It not block. Adds the wait queue to the table*/
poll_wait(pfile,&pmydev->rq,ptb);
poll_wait(pfile,&pmydev->wq,ptb);
mutex_lock(&pmydev->lock);
if(pmydev->curlen > 0)
{
mask |= POLLIN | POLLRDNORM;
}
if(pmydev->curlen < BUF_LEN)
{
mask |= POLLOUT | POLLWRNORM;
}
mutex_unlock(&pmydev->lock);
return mask;
}
int mychar_fasync(int fd,struct file *pfile, int mode)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
return fasync_helper(fd, pfile, mode, &pmydev->pasync_obj);
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = mychar_open,
.release = mychar_close,
.read = mychar_read,
.write = mychar_write,
.unlocked_ioctl = mychar_ioctl,
.poll = mychar_poll,
.fasync = mychar_fasync,
};
int __init mychar_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major,minor);
/*Apply for device number*/
ret = register_chrdev_region(devno, mychar_num, "mychar");
if(ret)
{
ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
if(ret)
{
printk("get devno failed\n");
return -1;
}
printk("copy_to_user failed\n");
major = MAJOR(devno);//Easy to miss *****
}
pgmydev = (struct mychar_dev *)kmalloc(sizeof(struct mychar_dev), GFP_KERNEL);
if(NULL == pgmydev)
{
unregister_chrdev_region(devno, mychar_num);
printk("kmallc for struct mychar_dev failed\n");
return -1;
}
/*Assign the 'struct cdev' a set of operation functions*/
cdev_init(&pgmydev->mydev, &myops);
/*Add 'struct cdev' to the kernel's data structure*/
pgmydev->mydev.owner = THIS_MODULE;
cdev_add(&pgmydev->mydev, devno, mychar_num);//add to Hash.
/*initialize the wait queue header*/
init_waitqueue_head(&pgmydev->rq);
init_waitqueue_head(&pgmydev->wq);
mutex_init(&pgmydev->lock);
return 0;
}
void __exit mychar_exit(void)
{
dev_t devno = MKDEV(major,minor);
cdev_del(&pgmydev->mydev);
//printk("mychar will exit\n");
unregister_chrdev_region(devno, mychar_num);
kfree(pgmydev);
pgmydev = NULL;
}
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);
三、IO访问-------访问外设控制器的寄存器
CPU不会直接和外设连接,需要经过一个对应的控制器,卡状的就叫适配器,芯片状的就叫控制器。外设直接和SOC上的控制器连接的叫一级外设。挂在总线上的叫二级外设。
两种方式:
1. IO端口:X86上用IO指令访问(in out)
2. IO内存:外设寄存器在SOC芯片手册上都有相应物理地址
IO内存访问接口:
(前两个函数是把物理地址转化成虚拟地址和取消虚拟地址和物理地址的关系
后面的函数是操作虚拟地址)
```c static inline void __iomem *ioremap(unsigned long offset, unsigned long size) /* 功能:实现IO管脚的映射 参数:offset:该管脚的偏移地址 Size:该管脚映射空间的大小 返回值:成功返回映射的虚拟地址,失败NULL */ static inline void iounmap(volatile void __iomem *addr) /* 功能:解除io管脚的映射 参数:addr:io管脚映射的地址 */ unsigned readb(void *addr);//1字节 或ioread8(void *addr) unsigned readw(void *addr);//2字节 或ioread16(void *addr) unsigned readl(void *addr);//4字节 或ioread32(void *addr) /* 功能:读取寄存器的值 参数:addr 地址 返回值:读到的数据 */ void writeb(unsigned value, void *addr);//1字节 或iowrite8(u8 value, void *addr) void writew(unsigned value, void *addr);//2字节 或iowrite16(u16 value, void *addr) void writel(unsigned value, void *addr);//4字节 或iowrite32(u32 value, void *addr) /* 功能:向指定的寄存器中,写入数据。 参数:value:待写入寄存器中的数据 Address:寄存器的虚拟地址 */ ```
四、led驱动
1. 读原理图
2. 查阅SOC芯片手册
GPX2_7 led2 GPX2CON----0x11000C40---28~31-----0001 GPX2DAT-----0x11000C44-----7
GPX1_0 led3 GPX1CON----0x11000C20---0~3-----0001 GPX1DAT----0x11000C24-----0
GPF3_4 led4 GPF3CON----0x114001E0---16~19-----0001 GPF3DAT----0x114001E4-----4
GPF3_5 led5 GPF3CON----0x114001E0---20~23-----0001 GPF3DAT----0x114001E4-----5
3. 编写驱动
a. 设计设备数据类型
```c
struct myled_dev
{
struct cdev mydev;
unsigned long * led2con;
unsigned long * led2dat;
unsigned long * led3con;
unsigned long * led3dat;
unsigned long * led4con;
unsigned long * led4dat;
unsigned long * led5con;
unsigned long * led5dat;
};
```
b. 考虑需要支持的函数
c. 模块入口:ioremap + 设置成输出
d. 模块出口:iounmap
e. 编写关灯函数和开灯函数,实现ioctl
volatile:对指针指向的空间不做任何优化,不加这个关键字会把指针的值放到cpu的寄存器中,加快访问速度。加上这个关键字会阻止这种优化
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include "leddrv.h"
int main(int argc,char *argv[])
{
int fd = -1;
int onoff = 0;
int no = 0;
if(argc < 4)
{
printf("The argument is too few\n");
return 1;
}
sscanf(argv[2],"%d",&onoff);
sscanf(argv[3],"%d",&no);
if(no < 2 || no > 5)
{
printf("len-no is invalid\n");
return 2;
}
fd = open(argv[1],O_RDONLY);
if(fd < 0)
{
printf("open %s failed\n",argv[1]);
return 3;
}
if(onoff)
{
ioctl(fd,MY_LED_ON,no);
}
else
{
ioctl(fd,MY_LED_OFF,no);
}
close(fd);
fd = -1;
return 0;
}
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/mm.h>
#include <linux/io.h>
#include <linux/slab.h>
#include "leddrv.h"
#define GPX1CON 0x11000c20
#define GPX1DAT 0x11000c24
#define GPX2CON 0x11000c40
#define GPX2DAT 0x11000c44
#define GPF3CON 0x114001E0
#define GPF3DAT 0x114001E4
int major = 11;
int minor = 0;
int myled_num = 1;
struct myled_dev
{
struct cdev mydev;
volatile unsigned long * pled2_con;
volatile unsigned long * pled2_dat;
volatile unsigned long * pled3_con;
volatile unsigned long * pled3_dat;
volatile unsigned long * pled4_con;
volatile unsigned long * pled4_dat;
volatile unsigned long * pled5_con;
volatile unsigned long * pled5_dat;
};
struct myled_dev *pgmydev = NULL;
int myled_open(struct inode *pnode, struct file *pfile)
{
pfile->private_data = container_of(pnode->i_cdev, struct myled_dev, mydev);
return 0;
}
int myled_close(struct inode *pnode, struct file *pfile)
{
//printk("myled_close\n");
/*C90 requires printk after the variable declaration*/
//struct myled_dev *pmydev = (struct myled_dev *)pfile->private_data;
return 0;
}
void led_on(struct myled_dev *pmydev,int ledno)
{
switch(ledno)
{
case 2:
writel(readl(pmydev->pled2_dat) | (0x1 << 7),pmydev->pled2_dat);
break;
case 3:
writel(readl(pmydev->pled3_dat) | (0x1),pmydev->pled3_dat);
break;
case 4:
writel(readl(pmydev->pled4_dat) | (0x1 << 4),pmydev->pled4_dat);
break;
case 5:
writel(readl(pmydev->pled5_dat) | (0x1 << 5),pmydev->pled5_dat);
break;
}
}
void led_off(struct myled_dev *pmydev,int ledno)
{
switch(ledno)
{
case 2:
writel(readl(pmydev->pled2_dat) & (~(0x1 << 7)),pmydev->pled2_dat);
break;
case 3:
writel(readl(pmydev->pled3_dat) & (~(0x1)),pmydev->pled3_dat);
break;
case 4:
writel(readl(pmydev->pled4_dat) & (~(0x1 << 4)),pmydev->pled4_dat);
break;
case 5:
writel(readl(pmydev->pled5_dat) & (~(0x1 << 5)),pmydev->pled5_dat);
break;
}
}
long myled_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
struct myled_dev *pmydev = (struct myled_dev *)pfile->private_data;
if(arg < 2 || arg > 5)
{
return -1;
}
switch(cmd)
{
case MY_LED_ON:
led_on(pmydev,arg);
break;
case MY_LED_OFF:
led_off(pmydev,arg);
break;
default:
return -1;
}
return 0;
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = myled_open,
.release = myled_close,
.unlocked_ioctl = myled_ioctl,
};
void ioremap_ledreg(struct myled_dev *pmydev)
{
pmydev->pled2_con = ioremap(GPX2CON,4);
pmydev->pled2_dat = ioremap(GPX2DAT,4);
pmydev->pled3_con = ioremap(GPX1CON,4);
pmydev->pled3_dat = ioremap(GPX1DAT,4);
pmydev->pled4_con = ioremap(GPF3CON,4);
pmydev->pled4_dat = ioremap(GPF3DAT,4);
pmydev->pled5_con = pmydev->pled4_con;
pmydev->pled5_dat = pmydev->pled4_dat;
}
void set_output_ledconreg(struct myled_dev *pmydev)
{
writel((readl(pmydev->pled2_con) & (~(0xF << 28))) | (0x1 << 28),pmydev->pled2_con);
writel((readl(pmydev->pled3_con) & (~(0xF))) | (0x1),pmydev->pled3_con);
writel((readl(pmydev->pled4_con) & (~(0xF << 16))) | (0x1 << 16),pmydev->pled4_con);
writel((readl(pmydev->pled5_con) & (~(0xF << 20))) | (0x1 << 20),pmydev->pled5_con);
writel(readl(pmydev->pled2_dat) & (~(0x1 << 7)),pmydev->pled2_dat);
writel(readl(pmydev->pled3_dat) & (~(0x1)),pmydev->pled3_dat);
writel(readl(pmydev->pled4_dat) & (~(0x1 << 4)),pmydev->pled4_dat);
writel(readl(pmydev->pled5_dat) & (~(0x1 << 5)),pmydev->pled5_dat);
}
void iounmap_ledreg(struct myled_dev *pmydev)
{
iounmap(pmydev->pled2_con);
pmydev->pled2_con = NULL;
iounmap(pmydev->pled2_dat);
pmydev->pled2_dat = NULL;
iounmap(pmydev->pled3_con);
pmydev->pled3_con = NULL;
iounmap(pmydev->pled3_dat);
pmydev->pled3_dat = NULL;
iounmap(pmydev->pled4_con);
pmydev->pled4_con = NULL;
iounmap(pmydev->pled4_dat);
pmydev->pled4_dat = NULL;
pmydev->pled5_con = NULL;
pmydev->pled5_dat = NULL;
}
int __init myled_init(void)
{
dev_t devno = MKDEV(major,minor);
int ret = 0;
/*Apply for device number*/
ret = register_chrdev_region(devno,myled_num,"myled");
if(ret)
{
ret = alloc_chrdev_region(&devno,minor,myled_num,"myled");
if(ret)
{
printk("get devno failed\n");
return -1;
}
major = MAJOR(devno);//Easy to miss *****
}
pgmydev = (struct myled_dev *)kmalloc(sizeof(struct myled_dev),GFP_KERNEL);
if(pgmydev == NULL)
{
unregister_chrdev_region(devno, myled_num);
printk("kmalloc failed\n");
return -1;
}
memset(pgmydev, 0, sizeof(struct myled_dev));
/*Assign the 'struct cdev' a set of operation functions*/
cdev_init(&pgmydev->mydev, &myops);
/*Add 'struct cdev' to the kernel's data structure*/
pgmydev->mydev.owner = THIS_MODULE;
cdev_add(&pgmydev->mydev, devno, myled_num);//add to Hash.
/*ioremap*/
ioremap_ledreg(pgmydev);
/*con-register set output*/
set_output_ledconreg(pgmydev);
return 0;
}
void __exit myled_exit(void)
{
dev_t devno = MKDEV(major,minor);
/*iounmap*/
iounmap_ledreg(pgmydev);
cdev_del(&pgmydev->mydev);
//printk("myled will exit\n");
unregister_chrdev_region(devno, myled_num);
kfree(pgmydev);
pgmydev = NULL;
}
MODULE_LICENSE("GPL");
module_init(myled_init);
module_exit(myled_exit);