1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/*
* Copyright (C) 2024 Free Software Foundation
*
* This program is free software ; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the program ; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <testlib.h>
#include <device/cons.h>
#include <mach/kern_return.h>
#include <mach/message.h>
#include <mach/mig_errors.h>
#include <mach/vm_param.h>
#include <sys/reboot.h>
#include <mach.user.h>
#include <mach_port.user.h>
#include <mach_host.user.h>
static int argc = 0;
static char *argv_unknown[] = {"unknown", "m1", "123", "456"};
static char **argv = argv_unknown;
static char **envp = NULL;
static int envc = 0;
static mach_port_t host_priv_port = 1;
static mach_port_t device_master_port = 2;
void cnputc(char c, vm_offset_t cookie)
{
char buf[2] = {c, 0};
mach_print(buf);
}
mach_port_t host_priv(void)
{
return host_priv_port;
}
mach_port_t device_priv(void)
{
return device_master_port;
}
void halt()
{
int ret = host_reboot(host_priv_port, RB_HALT);
ASSERT_RET(ret, "host_reboot() failed!");
while (1)
;
}
int msleep(uint32_t timeout)
{
mach_port_t recv = mach_reply_port();
return mach_msg(NULL, MACH_RCV_MSG|MACH_RCV_TIMEOUT|MACH_RCV_INTERRUPT,
0, 0, recv, timeout, MACH_PORT_NULL);
}
const char* e2s(int err)
{
const char* s = e2s_gnumach(err);
if (s != NULL)
return s;
else
switch (err)
{
default: return "unknown";
}
}
void mach_msg_destroy(mach_msg_header_t *msg)
{
mach_port_t tmp;
tmp = mach_reply_port();
msg->msgh_local_port = msg->msgh_remote_port;
msg->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND,
MACH_MSGH_BITS_REMOTE(msg->msgh_bits))
| MACH_MSGH_BITS_OTHER(msg->msgh_bits);
mach_msg(msg, MACH_SEND_MSG, msg->msgh_size, 0, MACH_PORT_NULL,
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
mach_port_mod_refs(mach_task_self(), tmp, MACH_PORT_RIGHT_RECEIVE, -1);
}
mach_msg_return_t mach_msg_server(
boolean_t (*demux) (mach_msg_header_t *request,
mach_msg_header_t *reply),
mach_msg_size_t max_size,
mach_port_t rcv_name,
mach_msg_option_t options)
{
mach_msg_return_t mr;
mig_reply_header_t *request;
mig_reply_header_t *reply;
mig_reply_header_t *tmp;
boolean_t handled;
request = __builtin_alloca(max_size);
reply = __builtin_alloca(max_size);
GetRequest:
mr = mach_msg(&request->Head, MACH_RCV_MSG|options,
0, max_size, rcv_name,
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
if (mr)
return mr;
Handle:
handled = demux(&request->Head, &reply->Head);
if (!handled)
reply->RetCode = MIG_BAD_ID;
if (reply->RetCode == MIG_NO_REPLY)
goto GetRequest;
else if (reply->RetCode != KERN_SUCCESS) {
request->Head.msgh_remote_port = MACH_PORT_NULL;
mach_msg_destroy(&request->Head);
}
if (!MACH_PORT_VALID(reply->Head.msgh_remote_port)) {
mach_msg_destroy(&reply->Head);
goto GetRequest;
}
tmp = request;
request = reply;
reply = tmp;
mr = mach_msg(&request->Head, MACH_SEND_MSG|MACH_RCV_MSG|options,
request->Head.msgh_size, max_size, rcv_name,
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
if (mr == MACH_MSG_SUCCESS)
goto Handle;
else if (mr == MACH_SEND_INVALID_DEST) {
mach_msg_destroy(&request->Head);
goto GetRequest;
}
return mr;
}
mach_msg_return_t mach_msg_server_once(
boolean_t (*demux) (mach_msg_header_t *request,
mach_msg_header_t *reply),
mach_msg_size_t max_size,
mach_port_t rcv_name,
mach_msg_option_t options)
{
mach_msg_return_t mr;
mig_reply_header_t *request;
mig_reply_header_t *reply;
boolean_t handled;
request = __builtin_alloca(max_size);
reply = __builtin_alloca(max_size);
mr = mach_msg(&request->Head, MACH_RCV_MSG|options,
0, max_size, rcv_name,
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
if (mr)
return mr;
handled = demux(&request->Head, &reply->Head);
if (!handled)
reply->RetCode = MIG_BAD_ID;
if (reply->RetCode == MIG_NO_REPLY)
return MACH_MSG_SUCCESS;
else if (reply->RetCode != KERN_SUCCESS) {
request->Head.msgh_remote_port = MACH_PORT_NULL;
mach_msg_destroy(&request->Head);
}
if (!MACH_PORT_VALID(reply->Head.msgh_remote_port)) {
mach_msg_destroy(&reply->Head);
return MACH_MSG_SUCCESS;
}
mr = mach_msg(&reply->Head, MACH_SEND_MSG|options,
reply->Head.msgh_size, 0, MACH_PORT_NULL,
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
if (mr == MACH_SEND_INVALID_DEST)
mach_msg_destroy(&reply->Head);
return mr;
}
/*
* Minimal _start() for test modules, we just take the arguments from the
* kernel, call main() and reboot. As in glibc, we expect the argument pointer
* as a first asrgument.
*/
void __attribute__((used, retain))
c_start(void **argptr)
{
intptr_t* argcptr = (intptr_t*)argptr;
argc = argcptr[0];
argv = (char **) &argcptr[1];
envp = &argv[argc + 1];
envc = 0;
while (envp[envc])
++envc;
mach_atoi(argv[1], &host_priv_port);
mach_atoi(argv[2], &device_master_port);
printf("started %s", argv[0]);
for (int i=1; i<argc; i++)
{
printf(" %s", argv[i]);
}
printf("\n");
int ret = main(argc, argv, envc, envp);
printf("%s: test %s exit code %x\n", TEST_SUCCESS_MARKER, argv[0], ret);
halt();
}
|