分类: LINUX
2011-03-25 14:37:00
相关专题:
对于提供了MMU(存储管理器,辅助操作系统进行内存管理,提供虚实地址转换等硬件支持)的处理器而言,Linux提供了复杂的存储管理系统,使得进程所能访问的内存达到4GB。
进程的4GB内存空间被人为的分为两个部分--用户空间与内核空间。用户空间地址分布从0到3GB(PAGE_OFFSET,在0x86中它等于0xC0000000),3GB到4GB为内核空间,如下图:
#define __pa(x) ((unsigned long)(x)-PAGE_OFFSET) extern inline unsigned long virt_to_phys(volatile void * address) { return __pa(address); } |
#define __va(x) ((void *)((unsigned long)(x)+PAGE_OFFSET)) extern inline void * phys_to_virt(unsigned long address) { return __va(address); } |
#include #include #include MODULE_LICENSE("GPL"); unsigned char *pagemem; unsigned char *kmallocmem; unsigned char *vmallocmem; int __init mem_module_init(void) { //最好每次内存申请都检查申请是否成功 //下面这段仅仅作为演示的代码没有检查 pagemem = (unsigned char*)get_free_page(0); printk("<1>pagemem addr=%x", pagemem); kmallocmem = (unsigned char*)kmalloc(100, 0); printk("<1>kmallocmem addr=%x", kmallocmem); vmallocmem = (unsigned char*)vmalloc(1000000); printk("<1>vmallocmem addr=%x", vmallocmem); return 0; } void __exit mem_module_exit(void) { free_page(pagemem); kfree(kmallocmem); vfree(vmallocmem); } module_init(mem_module_init); module_exit(mem_module_exit); |
void * ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags); |
void iounmap(void * addr); |
#define readb(addr) (*(volatile unsigned char *) __io_virt(addr)) #define readw(addr) (*(volatile unsigned short *) __io_virt(addr)) #define readl(addr) (*(volatile unsigned int *) __io_virt(addr)) #define writeb(b,addr) (*(volatile unsigned char *) __io_virt(addr) = (b)) #define writew(b,addr) (*(volatile unsigned short *) __io_virt(addr) = (b)) #define writel(b,addr) (*(volatile unsigned int *) __io_virt(addr) = (b)) #define memset_io(a,b,c) memset(__io_virt(a),(b),(c)) #define memcpy_fromio(a,b,c) memcpy((a),__io_virt(b),(c)) #define memcpy_toio(a,b,c) memcpy(__io_virt(a),(b),(c)) |
static void get_rtc_time(int alm, struct rtc_time *rtc_tm) { spin_lock_irq(&rtc_lock); if (alm == 1) { rtc_tm->tm_year = (unsigned char)ALMYEAR & Msk_RTCYEAR; rtc_tm->tm_mon = (unsigned char)ALMMON & Msk_RTCMON; rtc_tm->tm_mday = (unsigned char)ALMDAY & Msk_RTCDAY; rtc_tm->tm_hour = (unsigned char)ALMHOUR & Msk_RTCHOUR; rtc_tm->tm_min = (unsigned char)ALMMIN & Msk_RTCMIN; rtc_tm->tm_sec = (unsigned char)ALMSEC & Msk_RTCSEC; } else { read_rtc_bcd_time: rtc_tm->tm_year = (unsigned char)BCDYEAR & Msk_RTCYEAR; rtc_tm->tm_mon = (unsigned char)BCDMON & Msk_RTCMON; rtc_tm->tm_mday = (unsigned char)BCDDAY & Msk_RTCDAY; rtc_tm->tm_hour = (unsigned char)BCDHOUR & Msk_RTCHOUR; rtc_tm->tm_min = (unsigned char)BCDMIN & Msk_RTCMIN; rtc_tm->tm_sec = (unsigned char)BCDSEC & Msk_RTCSEC; if (rtc_tm->tm_sec == 0) { /* Re-read all BCD registers in case of BCDSEC is 0. See RTC section at the manual for more info. */ goto read_rtc_bcd_time; } } spin_unlock_irq(&rtc_lock); BCD_TO_BIN(rtc_tm->tm_year); BCD_TO_BIN(rtc_tm->tm_mon); BCD_TO_BIN(rtc_tm->tm_mday); BCD_TO_BIN(rtc_tm->tm_hour); BCD_TO_BIN(rtc_tm->tm_min); BCD_TO_BIN(rtc_tm->tm_sec); /* The epoch of tm_year is 1900 */ rtc_tm->tm_year += RTC_LEAP_YEAR - 1900; /* tm_mon starts at 0, but rtc month starts at 1 */ rtc_tm->tm_mon--; } |
#define ALMDAY bRTC(0x60) #define ALMMON bRTC(0x64) #define ALMYEAR bRTC(0x68) |
#define bRTC(Nb) __REG(0x57000000 + (Nb)) |
# define __REG(x) io_p2v(x) |
#define io_p2v(x) ((x) | 0xa0000000) |
# define __PREG(x) io_v2p(x) |
#define io_v2p(x) ((x) & ~0xa0000000) |
/************mmap_ioremap.c**************/ #include #include #include #include #include #include #include MODULE_PARM(mem_start, "i"); MODULE_PARM(mem_size, "i"); static int mem_start = 101, mem_size = 10; static char *reserve_virt_addr; static int major; int mmapdrv_open(struct inode *inode, struct file *file); int mmapdrv_release(struct inode *inode, struct file *file); int mmapdrv_mmap(struct file *file, struct vm_area_struct *vma); static struct file_operations mmapdrv_fops = { owner: THIS_MODULE, mmap: mmapdrv_mmap, open: mmapdrv_open, release: mmapdrv_release, }; int init_module(void) { if ((major = register_chrdev(0, "mmapdrv", &mmapdrv_fops)) < 0) { printk("mmapdrv: unable to register character device\n"); return ( - EIO); } printk("mmap device major = %d\n", major); printk("high memory physical address 0x%ldM\n", virt_to_phys(high_memory) / 1024 / 1024); reserve_virt_addr = ioremap(mem_start *1024 * 1024, mem_size *1024 * 1024); printk("reserve_virt_addr = 0x%lx\n", (unsigned long)reserve_virt_addr); if (reserve_virt_addr) { int i; for (i = 0; i < mem_size *1024 * 1024; i += 4) { reserve_virt_addr[i] = 'a'; reserve_virt_addr[i + 1] = 'b'; reserve_virt_addr[i + 2] = 'c'; reserve_virt_addr[i + 3] = 'd'; } } else { unregister_chrdev(major, "mmapdrv"); return - ENODEV; } return 0; } /* remove the module */ void cleanup_module(void) { if (reserve_virt_addr) iounmap(reserve_virt_addr); unregister_chrdev(major, "mmapdrv"); return ; } int mmapdrv_open(struct inode *inode, struct file *file) { MOD_INC_USE_COUNT; return (0); } int mmapdrv_release(struct inode *inode, struct file *file) { MOD_DEC_USE_COUNT; return (0); } int mmapdrv_mmap(struct file *file, struct vm_area_struct *vma) { unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; unsigned long size = vma->vm_end - vma->vm_start; if (size > mem_size *1024 * 1024) { printk("size too big\n"); return ( - ENXIO); } offset = offset + mem_start * 1024 * 1024; /* we do not want to have this area swapped out, lock it */ vma->vm_flags |= VM_LOCKED; if (remap_page_range(vma, vma->vm_start, offset, size, PAGE_SHARED)) { printk("remap page range failed\n"); return - ENXIO; } return (0); } |
int remap_page_range(vma_area_struct *vma, unsigned long from, unsigned long to, unsigned long size, pgprot_tprot); |