]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/phys.c
xnu-792.24.17.tar.gz
[apple/xnu.git] / osfmk / i386 / phys.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50
51 #include <mach_rt.h>
52 #include <mach_debug.h>
53 #include <mach_ldebug.h>
54
55 #include <sys/kdebug.h>
56
57 #include <mach/kern_return.h>
58 #include <mach/thread_status.h>
59 #include <mach/vm_param.h>
60
61 #include <kern/counters.h>
62 #include <kern/mach_param.h>
63 #include <kern/task.h>
64 #include <kern/thread.h>
65 #include <kern/sched_prim.h>
66 #include <kern/misc_protos.h>
67 #include <kern/assert.h>
68 #include <kern/spl.h>
69 #include <ipc/ipc_port.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_map.h>
72 #include <vm/pmap.h>
73
74 #include <i386/cpu_data.h>
75 #include <i386/cpu_number.h>
76 #include <i386/thread.h>
77 #include <i386/eflags.h>
78 #include <i386/proc_reg.h>
79 #include <i386/seg.h>
80 #include <i386/tss.h>
81 #include <i386/user_ldt.h>
82 #include <i386/fpu.h>
83 #include <i386/iopb_entries.h>
84 #include <i386/misc_protos.h>
85
86 /*
87 * pmap_zero_page zeros the specified (machine independent) page.
88 */
89 void
90 pmap_zero_page(
91 ppnum_t pn)
92 {
93 assert(pn != vm_page_fictitious_addr);
94 bzero_phys((addr64_t)i386_ptob(pn), PAGE_SIZE);
95 }
96
97 /*
98 * pmap_zero_part_page
99 * zeros the specified (machine independent) part of a page.
100 */
101 void
102 pmap_zero_part_page(
103 ppnum_t pn,
104 vm_offset_t offset,
105 vm_size_t len)
106 {
107 assert(pn != vm_page_fictitious_addr);
108 assert(offset + len <= PAGE_SIZE);
109 bzero_phys((addr64_t)(i386_ptob(pn) + offset), len);
110 }
111
112 /*
113 * pmap_copy_page copies the specified (machine independent) pages.
114 */
115 void
116 pmap_copy_part_page(
117 ppnum_t psrc,
118 vm_offset_t src_offset,
119 ppnum_t pdst,
120 vm_offset_t dst_offset,
121 vm_size_t len)
122 {
123 vm_offset_t src, dst;
124 assert(psrc != vm_page_fictitious_addr);
125 assert(pdst != vm_page_fictitious_addr);
126 src = (vm_offset_t)i386_ptob(psrc);
127 dst = (vm_offset_t)i386_ptob(pdst);
128 assert(((dst & PAGE_MASK) + dst_offset + len) <= PAGE_SIZE);
129 assert(((src & PAGE_MASK) + src_offset + len) <= PAGE_SIZE);
130 bcopy_phys((addr64_t)src + (src_offset & INTEL_OFFMASK),
131 (addr64_t)dst + (dst_offset & INTEL_OFFMASK),
132 len);
133 }
134
135 /*
136 * pmap_copy_part_lpage copies part of a virtually addressed page
137 * to a physically addressed page.
138 */
139 void
140 pmap_copy_part_lpage(
141 vm_offset_t src,
142 ppnum_t pdst,
143 vm_offset_t dst_offset,
144 vm_size_t len)
145 {
146 pt_entry_t *ptep;
147 thread_t thr_act = current_thread();
148
149 assert(pdst != vm_page_fictitious_addr);
150 ptep = pmap_pte(thr_act->map->pmap, i386_ptob(pdst));
151 if (0 == ptep)
152 panic("pmap_copy_part_lpage ptep");
153 assert(((pdst & PAGE_MASK) + dst_offset + len) <= PAGE_SIZE);
154 if (*(pt_entry_t *) CM2)
155 panic("pmap_copy_part_lpage");
156 *(int *) CM2 = INTEL_PTE_VALID | INTEL_PTE_RW | (*ptep & PG_FRAME) |
157 INTEL_PTE_REF | INTEL_PTE_MOD;
158 invlpg((unsigned int) CA2);
159 memcpy((void *) (CA2 + (dst_offset & INTEL_OFFMASK)), (void *) src, len);
160 *(pt_entry_t *) CM2 = 0;
161 }
162
163 /*
164 * pmap_copy_part_rpage copies part of a physically addressed page
165 * to a virtually addressed page.
166 */
167 void
168 pmap_copy_part_rpage(
169 ppnum_t psrc,
170 vm_offset_t src_offset,
171 vm_offset_t dst,
172 vm_size_t len)
173 {
174 pt_entry_t *ptep;
175 thread_t thr_act = current_thread();
176
177 assert(psrc != vm_page_fictitious_addr);
178 ptep = pmap_pte(thr_act->map->pmap, i386_ptob(psrc));
179 if (0 == ptep)
180 panic("pmap_copy_part_rpage ptep");
181 assert(((psrc & PAGE_MASK) + src_offset + len) <= PAGE_SIZE);
182 if (*(pt_entry_t *) CM2)
183 panic("pmap_copy_part_rpage");
184 *(pt_entry_t *) CM2 = INTEL_PTE_VALID | INTEL_PTE_RW | (*ptep & PG_FRAME) |
185 INTEL_PTE_REF;
186 invlpg((unsigned int) CA2);
187 memcpy((void *) dst, (void *) (CA2 + (src_offset & INTEL_OFFMASK)), len);
188 *(pt_entry_t *) CM2 = 0;
189 }
190
191 /*
192 * kvtophys(addr)
193 *
194 * Convert a kernel virtual address to a physical address
195 */
196 vm_offset_t
197 kvtophys(
198 vm_offset_t addr)
199 {
200 pt_entry_t *ptep;
201 pmap_paddr_t pa;
202
203 if ((ptep = pmap_pte(kernel_pmap, addr)) == PT_ENTRY_NULL) {
204 pa = 0;
205 } else {
206 pa = pte_to_pa(*ptep) | (addr & INTEL_OFFMASK);
207 }
208 if (0 == pa)
209 kprintf("kvtophys ret 0!\n");
210 return (pa);
211 }