1 /* $NetBSD: mm.h,v 1.3 2014/07/16 20:59:58 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Taylor R. Campbell. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef _LINUX_MM_H_ 33 #define _LINUX_MM_H_ 34 35 #include <sys/kauth.h> 36 #include <sys/file.h> 37 #include <sys/mman.h> 38 #include <sys/proc.h> 39 #include <sys/vnode.h> 40 41 #include <uvm/uvm_extern.h> 42 #include <uvm/uvm_map.h> 43 44 #include <asm/page.h> 45 46 /* XXX Ugh bletch! Whattakludge! Linux's sense is reversed... */ 47 #undef PAGE_MASK 48 #define PAGE_MASK (~(PAGE_SIZE-1)) 49 50 #define PAGE_ALIGN(x) (((x) + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1)) 51 #define offset_in_page(x) ((x) & (PAGE_SIZE-1)) 52 53 struct sysinfo { 54 unsigned long totalram; 55 unsigned long totalhigh; 56 uint32_t mem_unit; 57 }; 58 59 static inline void 60 si_meminfo(struct sysinfo *si) 61 { 62 63 si->totalram = uvmexp.npages; 64 si->totalhigh = kernel_map->size >> PAGE_SHIFT; 65 si->mem_unit = PAGE_SIZE; 66 /* XXX Fill in more as needed. */ 67 } 68 69 /* 70 * ################################################################### 71 * ############### XXX THIS NEEDS SERIOUS SCRUTINY XXX ############### 72 * ################################################################### 73 */ 74 75 /* 76 * XXX unsigned long is a loser but will probably work accidentally. 77 * XXX struct file might not map quite right between Linux and NetBSD. 78 * XXX This is large enough it should take its own file. 79 */ 80 81 static inline unsigned long 82 vm_mmap(struct file *file, unsigned long base, unsigned long size, 83 unsigned long prot, unsigned long flags, unsigned long token) 84 { 85 struct vnode *vnode; 86 vaddr_t addr; 87 int error; 88 89 /* 90 * Cargo-culted from sys_mmap. Various conditions kasserted 91 * rather than checked for expedience and safey. 92 */ 93 94 KASSERT(base == 0); 95 KASSERT(prot == (PROT_READ | PROT_WRITE)); 96 KASSERT(flags == MAP_SHARED); 97 98 KASSERT(file->f_type == DTYPE_VNODE); 99 vnode = file->f_data; 100 101 KASSERT(vnode->v_type == VCHR); 102 KASSERT((file->f_flag & (FREAD | FWRITE)) == (FREAD | FWRITE)); 103 104 { 105 struct vattr va; 106 107 vn_lock(vnode, (LK_SHARED | LK_RETRY)); 108 error = VOP_GETATTR(vnode, &va, kauth_cred_get()); 109 VOP_UNLOCK(vnode); 110 if (error) 111 goto out; 112 /* XXX kassert? */ 113 if ((va.va_flags & (SF_SNAPSHOT | IMMUTABLE | APPEND)) != 0) { 114 error = EACCES; 115 goto out; 116 } 117 } 118 119 /* XXX pax_mprotect? pax_aslr? */ 120 121 addr = (*curproc->p_emul->e_vm_default_addr)(curproc, 122 (vaddr_t)curproc->p_vmspace->vm_daddr, size); 123 error = uvm_mmap(&curproc->p_vmspace->vm_map, &addr, size, 124 (VM_PROT_READ | VM_PROT_WRITE), (VM_PROT_READ | VM_PROT_WRITE), 125 MAP_SHARED, vnode, base, 126 curproc->p_rlimit[RLIMIT_MEMLOCK].rlim_cur); 127 if (error) 128 goto out; 129 130 KASSERT(addr <= -1024UL); /* XXX Kludgerosity! */ 131 132 out: /* XXX errno NetBSD->Linux (kludgerific) */ 133 return (error? (-error) : (unsigned long)addr); 134 } 135 136 #endif /* _LINUX_MM_H_ */ 137