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