mm.h revision 1.1.2.4 1 /* $NetBSD: mm.h,v 1.1.2.4 2013/07/24 03:58:04 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
43 #define PAGE_ALIGN(x) round_page(x)
44
45 /* XXX Ugh bletch! Whattakludge! Linux's sense is reversed... */
46 #undef PAGE_MASK
47 #define PAGE_MASK (~(PAGE_SIZE-1))
48
49 /*
50 * ###################################################################
51 * ############### XXX THIS NEEDS SERIOUS SCRUTINY XXX ###############
52 * ###################################################################
53 */
54
55 /*
56 * XXX unsigned long is a loser but will probably work accidentally.
57 * XXX struct file might not map quite right between Linux and NetBSD.
58 * XXX This is large enough it should take its own file.
59 */
60
61 static inline unsigned long
62 vm_mmap(struct file *file, unsigned long base, unsigned long size,
63 unsigned long prot, unsigned long flags, unsigned long token)
64 {
65 struct vnode *vnode;
66 vaddr_t addr;
67 int error;
68
69 /*
70 * Cargo-culted from sys_mmap. Various conditions kasserted
71 * rather than checked for expedience and safey.
72 */
73
74 KASSERT(base == 0);
75 KASSERT(prot == (PROT_READ | PROT_WRITE));
76 KASSERT(flags == MAP_SHARED);
77
78 KASSERT(file->f_type == DTYPE_VNODE);
79 vnode = file->f_data;
80
81 KASSERT(vnode->v_type == VCHR);
82 KASSERT((file->f_flag & (FREAD | FWRITE)) == (FREAD | FWRITE));
83
84 {
85 struct vattr va;
86
87 vn_lock(vnode, (LK_SHARED | LK_RETRY));
88 error = VOP_GETATTR(vnode, &va, kauth_cred_get());
89 VOP_UNLOCK(vnode);
90 if (error)
91 goto out;
92 /* XXX kassert? */
93 if ((va.va_flags & (SF_SNAPSHOT | IMMUTABLE | APPEND)) != 0) {
94 error = EACCES;
95 goto out;
96 }
97 }
98
99 /* XXX pax_mprotect? pax_aslr? */
100
101 addr = (*curproc->p_emul->e_vm_default_addr)(curproc,
102 (vaddr_t)curproc->p_vmspace->vm_daddr, size);
103 error = uvm_mmap(&curproc->p_vmspace->vm_map, &addr, size,
104 (VM_PROT_READ | VM_PROT_WRITE), (VM_PROT_READ | VM_PROT_WRITE),
105 MAP_SHARED, vnode, base,
106 curproc->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
107 if (error)
108 goto out;
109
110 KASSERT(addr <= -1024UL); /* XXX Kludgerosity! */
111
112 out: /* XXX errno NetBSD->Linux (kludgerific) */
113 return (error? (-error) : (unsigned long)addr);
114 }
115
116 #endif /* _LINUX_MM_H_ */
117