⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mem.c

📁 linux和2410结合开发 用他可以生成2410所需的zImage文件
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  linux/drivers/char/mem.c * *  Copyright (C) 1991, 1992  Linus Torvalds * *  Added devfs support.  *    Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu> *  Shared /dev/zero mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com> */#include <linux/config.h>#include <linux/mm.h>#include <linux/miscdevice.h>#include <linux/tpqic02.h>#include <linux/ftape.h>#include <linux/slab.h>#include <linux/vmalloc.h>#include <linux/mman.h>#include <linux/random.h>#include <linux/init.h>#include <linux/raw.h>#include <linux/tty.h>#include <linux/capability.h>#include <asm/uaccess.h>#include <asm/io.h>#include <asm/pgalloc.h>#ifdef CONFIG_FBextern void fbmem_init(void);#endif#ifdef CONFIG_PROM_CONSOLEextern void prom_con_init(void);#endif#ifdef CONFIG_MDA_CONSOLEextern void mda_console_init(void);#endif#if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)extern void tapechar_init(void);#endif     static ssize_t do_write_mem(struct file * file, void *p, unsigned long realp,			    const char * buf, size_t count, loff_t *ppos){	ssize_t written;	written = 0;#if defined(__sparc__) || defined(__mc68000__)	/* we don't have page 0 mapped on sparc and m68k.. */	if (realp < PAGE_SIZE) {		unsigned long sz = PAGE_SIZE-realp;		if (sz > count) sz = count; 		/* Hmm. Do something? */		buf+=sz;		p+=sz;		count-=sz;		written+=sz;	}#endif	if (copy_from_user(p, buf, count))		return -EFAULT;	written += count;	*ppos += written;	return written;}/* * This funcion reads the *physical* memory. The f_pos points directly to the  * memory location.  */static ssize_t read_mem(struct file * file, char * buf,			size_t count, loff_t *ppos){	unsigned long p = *ppos;	unsigned long end_mem;	ssize_t read;		end_mem = __pa(high_memory);	if (p >= end_mem)		return 0;	if (count > end_mem - p)		count = end_mem - p;	read = 0;#if defined(__sparc__) || defined(__mc68000__)	/* we don't have page 0 mapped on sparc and m68k.. */	if (p < PAGE_SIZE) {		unsigned long sz = PAGE_SIZE-p;		if (sz > count) 			sz = count; 		if (sz > 0) {			if (clear_user(buf, sz))				return -EFAULT;			buf += sz; 			p += sz; 			count -= sz; 			read += sz; 		}	}#endif	if (copy_to_user(buf, __va(p), count))		return -EFAULT;	read += count;	*ppos += read;	return read;}static ssize_t write_mem(struct file * file, const char * buf, 			 size_t count, loff_t *ppos){	unsigned long p = *ppos;	unsigned long end_mem;	end_mem = __pa(high_memory);	if (p >= end_mem)		return 0;	if (count > end_mem - p)		count = end_mem - p;	return do_write_mem(file, __va(p), p, buf, count, ppos);}#ifndef pgprot_noncached/* * This should probably be per-architecture in <asm/pgtable.h> */static inline pgprot_t pgprot_noncached(pgprot_t _prot){	unsigned long prot = pgprot_val(_prot);#if defined(__i386__) || defined(__x86_64__)	/* On PPro and successors, PCD alone doesn't always mean 	    uncached because of interactions with the MTRRs. PCD | PWT	    means definitely uncached. */ 	if (boot_cpu_data.x86 > 3)		prot |= _PAGE_PCD | _PAGE_PWT;#elif defined(__powerpc__)	prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;#elif defined(__mc68000__)#ifdef SUN3_PAGE_NOCACHE	if (MMU_IS_SUN3)		prot |= SUN3_PAGE_NOCACHE;	else#endif	if (MMU_IS_851 || MMU_IS_030)		prot |= _PAGE_NOCACHE030;	/* Use no-cache mode, serialized */	else if (MMU_IS_040 || MMU_IS_060)		prot = (prot & _CACHEMASK040) | _PAGE_NOCACHE_S;#endif	return __pgprot(prot);}#endif /* !pgprot_noncached *//* * Architectures vary in how they handle caching for addresses  * outside of main memory. */static inline int noncached_address(unsigned long addr){#if defined(__i386__)	/* 	 * On the PPro and successors, the MTRRs are used to set	 * memory types for physical addresses outside main memory, 	 * so blindly setting PCD or PWT on those pages is wrong.	 * For Pentiums and earlier, the surround logic should disable 	 * caching for the high addresses through the KEN pin, but	 * we maintain the tradition of paranoia in this code.	 */ 	return !( test_bit(X86_FEATURE_MTRR, &boot_cpu_data.x86_capability) ||		  test_bit(X86_FEATURE_K6_MTRR, &boot_cpu_data.x86_capability) ||		  test_bit(X86_FEATURE_CYRIX_ARR, &boot_cpu_data.x86_capability) ||		  test_bit(X86_FEATURE_CENTAUR_MCR, &boot_cpu_data.x86_capability) )	  && addr >= __pa(high_memory);#else	return addr >= __pa(high_memory);#endif}static int mmap_mem(struct file * file, struct vm_area_struct * vma){	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;	/*	 * Accessing memory above the top the kernel knows about or	 * through a file pointer that was marked O_SYNC will be	 * done non-cached.	 */	if (noncached_address(offset) || (file->f_flags & O_SYNC))		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);	/* Don't try to swap out physical pages.. */	vma->vm_flags |= VM_RESERVED;	/*	 * Don't dump addresses that are not real memory to a core file.	 */	if (offset >= __pa(high_memory) || (file->f_flags & O_SYNC))		vma->vm_flags |= VM_IO;	if (remap_page_range(vma->vm_start, offset, vma->vm_end-vma->vm_start,			     vma->vm_page_prot))		return -EAGAIN;	return 0;}/* * This function reads the *virtual* memory as seen by the kernel. */static ssize_t read_kmem(struct file *file, char *buf, 			 size_t count, loff_t *ppos){	unsigned long p = *ppos;	ssize_t read = 0;	ssize_t virtr = 0;	char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */			if (p < (unsigned long) high_memory) {		read = count;		if (count > (unsigned long) high_memory - p)			read = (unsigned long) high_memory - p;#if defined(__sparc__) || defined(__mc68000__)		/* we don't have page 0 mapped on sparc and m68k.. */		if (p < PAGE_SIZE && read > 0) {			size_t tmp = PAGE_SIZE - p;			if (tmp > read) tmp = read;			if (clear_user(buf, tmp))				return -EFAULT;			buf += tmp;			p += tmp;			read -= tmp;			count -= tmp;		}#endif		if (copy_to_user(buf, (char *)p, read))			return -EFAULT;		p += read;		buf += read;		count -= read;	}	if (count > 0) {		kbuf = (char *)__get_free_page(GFP_KERNEL);		if (!kbuf)			return -ENOMEM;		while (count > 0) {			int len = count;			if (len > PAGE_SIZE)				len = PAGE_SIZE;			len = vread(kbuf, (char *)p, len);			if (!len)				break;			if (copy_to_user(buf, kbuf, len)) {				free_page((unsigned long)kbuf);				return -EFAULT;			}			count -= len;			buf += len;			virtr += len;			p += len;		}		free_page((unsigned long)kbuf);	} 	*ppos = p; 	return virtr + read;}extern long vwrite(char *buf, char *addr, unsigned long count);/* * This function writes to the *virtual* memory as seen by the kernel. */static ssize_t write_kmem(struct file * file, const char * buf, 			  size_t count, loff_t *ppos){	unsigned long p = *ppos;	ssize_t wrote = 0;	ssize_t virtr = 0;	char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */	if (p < (unsigned long) high_memory) {		wrote = count;		if (count > (unsigned long) high_memory - p)			wrote = (unsigned long) high_memory - p;		wrote = do_write_mem(file, (void*)p, p, buf, wrote, ppos);		p += wrote;		buf += wrote;		count -= wrote;	}	if (count > 0) {		kbuf = (char *)__get_free_page(GFP_KERNEL);		if (!kbuf)			return -ENOMEM;		while (count > 0) {			int len = count;			if (len > PAGE_SIZE)				len = PAGE_SIZE;			if (len && copy_from_user(kbuf, buf, len)) {				free_page((unsigned long)kbuf);				return -EFAULT;			}			len = vwrite(kbuf, (char *)p, len);			count -= len;			buf += len;			virtr += len;			p += len;		}		free_page((unsigned long)kbuf);	} 	*ppos = p; 	return virtr + wrote;}#if !defined(__mc68000__)static ssize_t read_port(struct file * file, char * buf,			 size_t count, loff_t *ppos){	unsigned long i = *ppos;	char *tmp = buf;	if (verify_area(VERIFY_WRITE,buf,count))		return -EFAULT; 	while (count-- > 0 && i < 65536) {		if (__put_user(inb(i),tmp) < 0) 			return -EFAULT;  		i++;		tmp++;	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -