1/*
2 * Copyright (c) 2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/*
30 * Make sure we don't accidentally include the external definitions of
31 * the routines we're interposing on below.
32 */
33#define _vm_map_user_
34#define _mach_vm_user_
35#include <mach/mach.h>
36#include <mach/mach_traps.h>
37#undef _vm_map_user_
38#include <mach/vm_map_internal.h>
39#undef _mach_vm_user_
40#include <mach/mach_vm_internal.h>
41
42kern_return_t
43mach_vm_allocate(
44		vm_map_t target,
45		mach_vm_address_t *address,
46		mach_vm_size_t size,
47		int flags)
48{
49	kern_return_t rv;
50
51	rv = _kernelrpc_mach_vm_allocate_trap(target, address, size, flags);
52
53	if (rv == MACH_SEND_INVALID_DEST)
54		rv = _kernelrpc_mach_vm_allocate(target, address, size, flags);
55
56	return (rv);
57}
58
59kern_return_t
60mach_vm_deallocate(
61	vm_map_t target,
62	mach_vm_address_t address,
63	mach_vm_size_t size)
64{
65	kern_return_t rv;
66
67	rv = _kernelrpc_mach_vm_deallocate_trap(target, address, size);
68
69	if (rv == MACH_SEND_INVALID_DEST)
70		rv = _kernelrpc_mach_vm_deallocate(target, address, size);
71
72	return (rv);
73}
74
75kern_return_t
76mach_vm_protect(
77	vm_map_t task,
78	mach_vm_address_t address,
79	mach_vm_size_t size,
80	boolean_t set_maximum,
81	vm_prot_t new_protection)
82{
83	kern_return_t rv;
84
85	rv = _kernelrpc_mach_vm_protect_trap(task, address, size, set_maximum,
86		new_protection);
87
88	if (rv == MACH_SEND_INVALID_DEST)
89		rv = _kernelrpc_mach_vm_protect(task, address, size,
90			set_maximum, new_protection);
91
92	return (rv);
93}
94
95kern_return_t
96vm_allocate(
97	vm_map_t task,
98	vm_address_t *address,
99	vm_size_t size,
100	int flags)
101{
102	kern_return_t rv;
103	mach_vm_address_t mach_addr;
104
105	mach_addr = (mach_vm_address_t)*address;
106	rv = mach_vm_allocate(task, &mach_addr, size, flags);
107#if defined(__LP64__)
108	*address = mach_addr;
109#else
110	*address = (vm_address_t)(mach_addr & ((vm_address_t)-1));
111#endif
112
113	return (rv);
114}
115
116kern_return_t
117vm_deallocate(
118	vm_map_t task,
119	vm_address_t address,
120	vm_size_t size)
121{
122	kern_return_t rv;
123
124	rv = mach_vm_deallocate(task, address, size);
125
126	return (rv);
127}
128
129kern_return_t
130vm_protect(
131	vm_map_t task,
132	vm_address_t address,
133	vm_size_t size,
134	boolean_t set_maximum,
135	vm_prot_t new_protection)
136{
137	kern_return_t rv;
138
139	rv = mach_vm_protect(task, address, size, set_maximum, new_protection);
140
141	return (rv);
142}
143