mem.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 734 行 · 第 1/2 页

C
734
字号
/* *  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/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 <linux/smp_lock.h>#include <linux/devfs_fs_kernel.h>#include <linux/ptrace.h>#include <linux/device.h>#include <asm/uaccess.h>#include <asm/io.h>#ifdef CONFIG_IA64# include <linux/efi.h>#endif#if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)extern void tapechar_init(void);#endif/* * Architectures vary in how they handle caching for addresses * outside of main memory. * */static inline int uncached_access(struct file *file, 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.	 */	if (file->f_flags & O_SYNC)		return 1; 	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);#elif defined(__x86_64__)	/* 	 * This is broken because it can generate memory type aliases,	 * which can cause cache corruptions	 * But it is only available for root and we have to be bug-to-bug	 * compatible with i386.	 */	if (file->f_flags & O_SYNC)		return 1;	/* same behaviour as i386. PAT always set to cached and MTRRs control the	   caching behaviour. 	   Hopefully a full PAT implementation will fix that soon. */	   	return 0;#elif defined(CONFIG_IA64)	/*	 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.	 */	return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);#elif defined(CONFIG_PPC64)	/* On PPC64, we always do non-cacheable access to the IO hole and	 * cacheable elsewhere. Cache paradox can checkstop the CPU and	 * the high_memory heuristic below is wrong on machines with memory	 * above the IO hole... Ah, and of course, XFree86 doesn't pass	 * O_SYNC when mapping us to tap IO space. Surprised ?	 */	return !page_is_ram(addr >> PAGE_SHIFT);#else	/*	 * 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 (file->f_flags & O_SYNC)		return 1;	return addr >= __pa(high_memory);#endif}#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGEstatic inline int valid_phys_addr_range(unsigned long addr, size_t *count){	unsigned long end_mem;	end_mem = __pa(high_memory);	if (addr >= end_mem)		return 0;	if (*count > end_mem - addr)		*count = end_mem - addr;	return 1;}#endifstatic ssize_t do_write_mem(void *p, unsigned long realp,			    const char __user * buf, size_t count, loff_t *ppos){	ssize_t written;	unsigned long copied;	written = 0;#if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))	/* 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	copied = copy_from_user(p, buf, count);	if (copied) {		ssize_t ret = written + (count - copied);		if (ret)			return ret;		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 __user * buf,			size_t count, loff_t *ppos){	unsigned long p = *ppos;	ssize_t read;	if (!valid_phys_addr_range(p, &count))		return -EFAULT;	read = 0;#if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))	/* 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 __user * buf, 			 size_t count, loff_t *ppos){	unsigned long p = *ppos;	if (!valid_phys_addr_range(p, &count))		return -EFAULT;	return do_write_mem(__va(p), p, buf, count, ppos);}static int mmap_mem(struct file * file, struct vm_area_struct * vma){	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;	int uncached;	uncached = uncached_access(file, offset);#ifdef pgprot_noncached	if (uncached)		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);#endif	/* 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 (uncached)		vma->vm_flags |= VM_IO;	if (remap_page_range(vma, vma->vm_start, offset, vma->vm_end-vma->vm_start,			     vma->vm_page_prot))		return -EAGAIN;	return 0;}extern long vread(char *buf, char *addr, unsigned long count);extern long vwrite(char *buf, char *addr, unsigned long count);/* * This function reads the *virtual* memory as seen by the kernel. */static ssize_t read_kmem(struct file *file, char __user *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__) && defined(CONFIG_MMU))		/* 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;}/* * This function writes to the *virtual* memory as seen by the kernel. */static ssize_t write_kmem(struct file * file, const char __user * buf, 			  size_t count, loff_t *ppos){	unsigned long p = *ppos;	ssize_t wrote = 0;	ssize_t virtr = 0;	ssize_t written;	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;		written = do_write_mem((void*)p, p, buf, wrote, ppos);		if (written != wrote)			return written;		wrote = written;		p += wrote;		buf += wrote;		count -= wrote;	}	if (count > 0) {		kbuf = (char *)__get_free_page(GFP_KERNEL);		if (!kbuf)			return wrote ? wrote : -ENOMEM;		while (count > 0) {			int len = count;			if (len > PAGE_SIZE)				len = PAGE_SIZE;			if (len) {				written = copy_from_user(kbuf, buf, len);				if (written) {					ssize_t ret;					free_page((unsigned long)kbuf);					ret = wrote + virtr + (len - written);					return ret ? ret : -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(CONFIG_ISA) || !defined(__mc68000__)static ssize_t read_port(struct file * file, char __user * buf,			 size_t count, loff_t *ppos){	unsigned long i = *ppos;	char __user *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++;	}	*ppos = i;	return tmp-buf;}static ssize_t write_port(struct file * file, const char __user * buf,			  size_t count, loff_t *ppos){	unsigned long i = *ppos;	const char __user * tmp = buf;

⌨️ 快捷键说明

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