Rev 2894 | Rev 2897 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2894 | Rev 2896 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jiri Svoboda |
2 | * Copyright (c) 2008 Jiri Svoboda |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup generic |
29 | /** @addtogroup generic |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Udebug operations. |
35 | * @brief Udebug operations. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <console/klog.h> |
38 | #include <console/klog.h> |
39 | #include <proc/task.h> |
39 | #include <proc/task.h> |
40 | #include <proc/thread.h> |
40 | #include <proc/thread.h> |
41 | #include <arch.h> |
41 | #include <arch.h> |
42 | #include <errno.h> |
42 | #include <errno.h> |
43 | #include <syscall/copy.h> |
43 | #include <syscall/copy.h> |
44 | #include <ipc/ipc.h> |
44 | #include <ipc/ipc.h> |
45 | #include <udebug/udebug.h> |
45 | #include <udebug/udebug.h> |
46 | #include <udebug/udebug_ops.h> |
46 | #include <udebug/udebug_ops.h> |
47 | 47 | ||
48 | /** |
48 | /** |
49 | * Prepare a thread for a debugging operation. |
49 | * Prepare a thread for a debugging operation. |
50 | * |
50 | * |
51 | * Simply put, return thread t with t->debug_lock held, |
51 | * Simply put, return thread t with t->debug_lock held, |
52 | * but only if it verifies all conditions. |
52 | * but only if it verifies all conditions. |
53 | * |
53 | * |
54 | * Specifically, verifies that thread t exists, is a userspace thread, |
54 | * Specifically, verifies that thread t exists, is a userspace thread, |
55 | * and belongs to the current task (TASK). It also locks t->debug_lock, |
55 | * and belongs to the current task (TASK). It also locks t->debug_lock, |
56 | * making sure that t->debug_active is true - that the thread is |
56 | * making sure that t->debug_active is true - that the thread is |
57 | * in a valid debugging session. |
57 | * in a valid debugging session. |
58 | * |
58 | * |
59 | * Returns EOK if all went well, or an error code otherwise. |
59 | * Returns EOK if all went well, or an error code otherwise. |
60 | * Interrupts must be already disabled when calling this function. |
60 | * Interrupts must be already disabled when calling this function. |
61 | * |
61 | * |
62 | * Note: This function sports complicated locking. |
62 | * Note: This function sports complicated locking. |
63 | */ |
63 | */ |
64 | static int _thread_op_begin(thread_t *t) |
64 | static int _thread_op_begin(thread_t *t) |
65 | { |
65 | { |
66 | int rc; |
66 | int rc; |
67 | task_id_t taskid; |
67 | task_id_t taskid; |
68 | 68 | ||
69 | taskid = TASK->taskid; |
69 | taskid = TASK->taskid; |
70 | 70 | ||
71 | /* Must lock threads_lock to ensure continued existence of the thread */ |
71 | /* Must lock threads_lock to ensure continued existence of the thread */ |
72 | spinlock_lock(&threads_lock); |
72 | spinlock_lock(&threads_lock); |
73 | 73 | ||
74 | if (!thread_exists(t)) { |
74 | if (!thread_exists(t)) { |
75 | spinlock_unlock(&threads_lock); |
75 | spinlock_unlock(&threads_lock); |
76 | return ENOENT; |
76 | return ENOENT; |
77 | } |
77 | } |
78 | 78 | ||
79 | spinlock_lock(&t->debug_lock); |
79 | spinlock_lock(&t->debug_lock); |
80 | spinlock_lock(&t->lock); |
80 | spinlock_lock(&t->lock); |
81 | 81 | ||
82 | /* Now verify that it's the current task */ |
82 | /* Now verify that it's the current task */ |
83 | if (t->task != TASK) { |
83 | if (t->task != TASK) { |
84 | /* No such thread belonging to callee */ |
84 | /* No such thread belonging to callee */ |
85 | rc = ENOENT; |
85 | rc = ENOENT; |
86 | goto error_exit; |
86 | goto error_exit; |
87 | } |
87 | } |
88 | 88 | ||
89 | /* Verify that 't' is a userspace thread */ |
89 | /* Verify that 't' is a userspace thread */ |
90 | if ((t->flags & THREAD_FLAG_USPACE) == 0) { |
90 | if ((t->flags & THREAD_FLAG_USPACE) == 0) { |
91 | /* It's not, deny its existence */ |
91 | /* It's not, deny its existence */ |
92 | rc = ENOENT; |
92 | rc = ENOENT; |
93 | goto error_exit; |
93 | goto error_exit; |
94 | } |
94 | } |
95 | 95 | ||
96 | if ((t->debug_active != true) || (t->debug_stop != true)) { |
96 | if ((t->debug_active != true) || (t->debug_stop != true)) { |
97 | /* Not in debugging session or already has GO */ |
97 | /* Not in debugging session or already has GO */ |
98 | rc = ENOENT; |
98 | rc = ENOENT; |
99 | goto error_exit; |
99 | goto error_exit; |
100 | } |
100 | } |
101 | 101 | ||
102 | spinlock_unlock(&threads_lock); |
102 | spinlock_unlock(&threads_lock); |
103 | spinlock_unlock(&t->lock); |
103 | spinlock_unlock(&t->lock); |
104 | 104 | ||
105 | /* Only t->debug_lock left */ |
105 | /* Only t->debug_lock left */ |
106 | 106 | ||
107 | return EOK; /* All went well */ |
107 | return EOK; /* All went well */ |
108 | 108 | ||
109 | 109 | ||
110 | /* Executed when a check on the thread fails */ |
110 | /* Executed when a check on the thread fails */ |
111 | error_exit: |
111 | error_exit: |
112 | spinlock_unlock(&t->lock); |
112 | spinlock_unlock(&t->lock); |
113 | spinlock_unlock(&t->debug_lock); |
113 | spinlock_unlock(&t->debug_lock); |
114 | spinlock_unlock(&threads_lock); |
114 | spinlock_unlock(&threads_lock); |
115 | 115 | ||
116 | /* No locks left here */ |
116 | /* No locks left here */ |
117 | return rc; /* Some errors occured */ |
117 | return rc; /* Some errors occured */ |
118 | } |
118 | } |
119 | 119 | ||
120 | 120 | ||
121 | static void _thread_op_end(thread_t *t) |
121 | static void _thread_op_end(thread_t *t) |
122 | { |
122 | { |
123 | spinlock_unlock(&t->debug_lock); |
123 | spinlock_unlock(&t->debug_lock); |
124 | } |
124 | } |
125 | 125 | ||
126 | /** |
126 | /** |
127 | * \return 0 (ok, but not done yet), 1 (done) or negative error code. |
127 | * \return 0 (ok, but not done yet), 1 (done) or negative error code. |
128 | */ |
128 | */ |
129 | int udebug_begin(call_t *call) |
129 | int udebug_begin(call_t *call) |
130 | { |
130 | { |
131 | ipl_t ipl; |
131 | ipl_t ipl; |
132 | int reply; |
132 | int reply; |
133 | 133 | ||
134 | thread_t *t; |
134 | thread_t *t; |
135 | link_t *cur; |
135 | link_t *cur; |
136 | 136 | ||
137 | klog_printf("udebug_begin()"); |
137 | klog_printf("udebug_begin()"); |
138 | 138 | ||
139 | ipl = interrupts_disable(); |
139 | ipl = interrupts_disable(); |
140 | klog_printf("debugging task %llu", TASK->taskid); |
140 | klog_printf("debugging task %llu", TASK->taskid); |
141 | 141 | ||
142 | spinlock_lock(&TASK->lock); |
142 | spinlock_lock(&TASK->lock); |
143 | 143 | ||
144 | if (TASK->dt_state != UDEBUG_TS_INACTIVE) { |
144 | if (TASK->dt_state != UDEBUG_TS_INACTIVE) { |
145 | spinlock_unlock(&TASK->lock); |
145 | spinlock_unlock(&TASK->lock); |
146 | interrupts_restore(ipl); |
146 | interrupts_restore(ipl); |
147 | klog_printf("udebug_begin(): busy error"); |
147 | klog_printf("udebug_begin(): busy error"); |
148 | 148 | ||
149 | return EBUSY; |
149 | return EBUSY; |
150 | } |
150 | } |
151 | 151 | ||
152 | TASK->dt_state = UDEBUG_TS_BEGINNING; |
152 | TASK->dt_state = UDEBUG_TS_BEGINNING; |
153 | TASK->debug_begin_call = call; |
153 | TASK->debug_begin_call = call; |
154 | TASK->debugger = call->sender; |
154 | TASK->debugger = call->sender; |
155 | 155 | ||
156 | if (TASK->not_stoppable_count == 0) { |
156 | if (TASK->not_stoppable_count == 0) { |
157 | TASK->dt_state = UDEBUG_TS_ACTIVE; |
157 | TASK->dt_state = UDEBUG_TS_ACTIVE; |
158 | TASK->debug_begin_call = NULL; |
158 | TASK->debug_begin_call = NULL; |
159 | reply = 1; /* immediate reply */ |
159 | reply = 1; /* immediate reply */ |
160 | } else { |
160 | } else { |
161 | reply = 0; /* no reply */ |
161 | reply = 0; /* no reply */ |
162 | } |
162 | } |
163 | 163 | ||
164 | /* Set debug_active on all of the task's userspace threads */ |
164 | /* Set debug_active on all of the task's userspace threads */ |
165 | 165 | ||
166 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
166 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
167 | t = list_get_instance(cur, thread_t, th_link); |
167 | t = list_get_instance(cur, thread_t, th_link); |
168 | 168 | ||
169 | spinlock_lock(&t->debug_lock); |
169 | spinlock_lock(&t->debug_lock); |
170 | if ((t->flags & THREAD_FLAG_USPACE) != 0) |
170 | if ((t->flags & THREAD_FLAG_USPACE) != 0) |
171 | t->debug_active = true; |
171 | t->debug_active = true; |
172 | spinlock_unlock(&t->debug_lock); |
172 | spinlock_unlock(&t->debug_lock); |
173 | } |
173 | } |
174 | 174 | ||
175 | spinlock_unlock(&TASK->lock); |
175 | spinlock_unlock(&TASK->lock); |
176 | interrupts_restore(ipl); |
176 | interrupts_restore(ipl); |
177 | 177 | ||
178 | klog_printf("udebug_begin() done (%s)", |
178 | klog_printf("udebug_begin() done (%s)", |
179 | reply ? "reply" : "stoppability wait"); |
179 | reply ? "reply" : "stoppability wait"); |
180 | 180 | ||
181 | return reply; |
181 | return reply; |
182 | } |
182 | } |
183 | 183 | ||
184 | int udebug_end(void) |
184 | int udebug_end(void) |
185 | { |
185 | { |
186 | ipl_t ipl; |
186 | ipl_t ipl; |
187 | int rc; |
187 | int rc; |
188 | 188 | ||
189 | klog_printf("udebug_end()"); |
189 | klog_printf("udebug_end()"); |
190 | 190 | ||
191 | ipl = interrupts_disable(); |
191 | ipl = interrupts_disable(); |
192 | spinlock_lock(&TASK->lock); |
192 | spinlock_lock(&TASK->lock); |
193 | 193 | ||
194 | rc = udebug_task_cleanup(TASK); |
194 | rc = udebug_task_cleanup(TASK); |
195 | 195 | ||
196 | klog_printf("task %llu", TASK->taskid); |
196 | klog_printf("task %llu", TASK->taskid); |
197 | 197 | ||
198 | spinlock_unlock(&TASK->lock); |
198 | spinlock_unlock(&TASK->lock); |
199 | interrupts_restore(ipl); |
199 | interrupts_restore(ipl); |
200 | 200 | ||
201 | if (rc < 0) return EINVAL; |
201 | if (rc < 0) return EINVAL; |
202 | 202 | ||
203 | return 0; |
203 | return 0; |
204 | } |
204 | } |
205 | 205 | ||
206 | int udebug_go(thread_t *t, call_t *call) |
206 | int udebug_go(thread_t *t, call_t *call) |
207 | { |
207 | { |
208 | ipl_t ipl; |
208 | ipl_t ipl; |
209 | int rc; |
209 | int rc; |
210 | 210 | ||
211 | klog_printf("udebug_go()"); |
211 | klog_printf("udebug_go()"); |
212 | 212 | ||
213 | ipl = interrupts_disable(); |
213 | ipl = interrupts_disable(); |
214 | 214 | ||
215 | /* On success, this will lock t->debug_lock */ |
215 | /* On success, this will lock t->debug_lock */ |
216 | rc = _thread_op_begin(t); |
216 | rc = _thread_op_begin(t); |
217 | if (rc != EOK) { |
217 | if (rc != EOK) { |
218 | interrupts_restore(ipl); |
218 | interrupts_restore(ipl); |
219 | return rc; |
219 | return rc; |
220 | } |
220 | } |
221 | 221 | ||
222 | t->debug_go_call = call; |
222 | t->debug_go_call = call; |
223 | t->debug_stop = false; |
223 | t->debug_stop = false; |
224 | t->cur_event = 0; /* none */ |
224 | t->cur_event = 0; /* none */ |
225 | 225 | ||
226 | /* |
226 | /* |
227 | * Neither t's lock nor threads_lock may be held during wakeup |
227 | * Neither t's lock nor threads_lock may be held during wakeup |
228 | */ |
228 | */ |
229 | waitq_wakeup(&t->go_wq, WAKEUP_FIRST); |
229 | waitq_wakeup(&t->go_wq, WAKEUP_FIRST); |
230 | 230 | ||
231 | _thread_op_end(t); |
231 | _thread_op_end(t); |
232 | interrupts_restore(ipl); |
232 | interrupts_restore(ipl); |
233 | 233 | ||
234 | return 0; |
234 | return 0; |
235 | } |
235 | } |
236 | 236 | ||
237 | 237 | ||
238 | int udebug_thread_read(void **buffer, size_t *n) |
238 | int udebug_thread_read(void **buffer, size_t *n) |
239 | { |
239 | { |
240 | thread_t *t; |
240 | thread_t *t; |
241 | link_t *cur; |
241 | link_t *cur; |
242 | unative_t tid; |
242 | unative_t tid; |
243 | unsigned num_threads, copied_ids; |
243 | unsigned num_threads, copied_ids; |
244 | ipl_t ipl; |
244 | ipl_t ipl; |
245 | unative_t *id_buffer; |
245 | unative_t *id_buffer; |
246 | int flags; |
246 | int flags; |
247 | 247 | ||
248 | klog_printf("udebug_thread_read()"); |
248 | klog_printf("udebug_thread_read()"); |
249 | 249 | ||
250 | ipl = interrupts_disable(); |
250 | ipl = interrupts_disable(); |
251 | spinlock_lock(&TASK->lock); |
251 | spinlock_lock(&TASK->lock); |
252 | 252 | ||
253 | /* Verify task state */ |
253 | /* Verify task state */ |
254 | if (TASK->dt_state != UDEBUG_TS_ACTIVE) { |
254 | if (TASK->dt_state != UDEBUG_TS_ACTIVE) { |
255 | spinlock_unlock(&TASK->lock); |
255 | spinlock_unlock(&TASK->lock); |
256 | interrupts_restore(ipl); |
256 | interrupts_restore(ipl); |
257 | 257 | ||
258 | return EINVAL; |
258 | return EINVAL; |
259 | } |
259 | } |
260 | 260 | ||
261 | /* Count the threads first */ |
261 | /* Count the threads first */ |
262 | 262 | ||
263 | num_threads = 0; |
263 | num_threads = 0; |
264 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
264 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
265 | /* Count all threads, to be on the safe side */ |
265 | /* Count all threads, to be on the safe side */ |
266 | ++num_threads; |
266 | ++num_threads; |
267 | } |
267 | } |
268 | 268 | ||
269 | /* Allocate a buffer and copy down the threads' ids */ |
269 | /* Allocate a buffer and copy down the threads' ids */ |
- | 270 | //FIXME!!! must not malloc when locks are held |
|
270 | id_buffer = malloc(num_threads * sizeof(unative_t), 0); // ??? |
271 | id_buffer = malloc(num_threads * sizeof(unative_t), 0); |
- | 272 | if (!id_buffer) { |
|
- | 273 | spinlock_unlock(&TASK->lock); |
|
- | 274 | interrupts_restore(ipl); |
|
- | 275 | ||
- | 276 | return ENOMEM; |
|
- | 277 | } |
|
271 | 278 | ||
272 | copied_ids = 0; |
279 | copied_ids = 0; |
273 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
280 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
274 | t = list_get_instance(cur, thread_t, th_link); |
281 | t = list_get_instance(cur, thread_t, th_link); |
275 | 282 | ||
276 | spinlock_lock(&t->lock); |
283 | spinlock_lock(&t->lock); |
277 | flags = t->flags; |
284 | flags = t->flags; |
278 | spinlock_unlock(&t->lock); |
285 | spinlock_unlock(&t->lock); |
279 | 286 | ||
280 | /* Not interested in kernel threads */ |
287 | /* Not interested in kernel threads */ |
281 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
288 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
282 | /* Using thread struct pointer for identification */ |
289 | /* Using thread struct pointer for identification */ |
283 | tid = (unative_t) t; |
290 | tid = (unative_t) t; |
284 | id_buffer[copied_ids++] = tid; |
291 | id_buffer[copied_ids++] = tid; |
285 | } |
292 | } |
286 | } |
293 | } |
287 | 294 | ||
288 | spinlock_unlock(&TASK->lock); |
295 | spinlock_unlock(&TASK->lock); |
289 | interrupts_restore(ipl); |
296 | interrupts_restore(ipl); |
290 | 297 | ||
291 | *buffer = id_buffer; |
298 | *buffer = id_buffer; |
292 | *n = copied_ids * sizeof(unative_t); |
299 | *n = copied_ids * sizeof(unative_t); |
293 | 300 | ||
294 | return 0; |
301 | return 0; |
295 | } |
302 | } |
296 | 303 | ||
297 | int udebug_args_read(thread_t *t, void **buffer) |
304 | int udebug_args_read(thread_t *t, void **buffer) |
298 | { |
305 | { |
299 | int rc; |
306 | int rc; |
300 | ipl_t ipl; |
307 | ipl_t ipl; |
301 | unative_t *arg_buffer; |
308 | unative_t *arg_buffer; |
302 | 309 | ||
303 | klog_printf("udebug_args_read()"); |
310 | klog_printf("udebug_args_read()"); |
304 | 311 | ||
- | 312 | /* Prepare a buffer to hold the arguments */ |
|
- | 313 | arg_buffer = malloc(6 * sizeof(unative_t), 0); |
|
- | 314 | if (!arg_buffer) return ENOMEM; |
|
- | 315 | ||
305 | ipl = interrupts_disable(); |
316 | ipl = interrupts_disable(); |
306 | 317 | ||
307 | /* On success, this will lock t->debug_lock */ |
318 | /* On success, this will lock t->debug_lock */ |
308 | rc = _thread_op_begin(t); |
319 | rc = _thread_op_begin(t); |
309 | if (rc != EOK) { |
320 | if (rc != EOK) { |
310 | interrupts_restore(ipl); |
321 | interrupts_restore(ipl); |
311 | return rc; |
322 | return rc; |
312 | } |
323 | } |
313 | 324 | ||
314 | /* Additionally we need to verify that we are inside a syscall */ |
325 | /* Additionally we need to verify that we are inside a syscall */ |
315 | if (t->cur_event != UDEBUG_EVENT_SYSCALL) { |
326 | if (t->cur_event != UDEBUG_EVENT_SYSCALL) { |
316 | _thread_op_end(t); |
327 | _thread_op_end(t); |
317 | interrupts_restore(ipl); |
328 | interrupts_restore(ipl); |
318 | 329 | ||
319 | return EINVAL; |
330 | return EINVAL; |
320 | } |
331 | } |
321 | 332 | ||
322 | /* Copy to a local buffer before releasing the lock */ |
333 | /* Copy to a local buffer before releasing the lock */ |
323 | arg_buffer = malloc(6 * sizeof(unative_t), 0); // ??? |
- | |
324 | memcpy(arg_buffer, t->syscall_args, 6 * sizeof(unative_t)); |
334 | memcpy(arg_buffer, t->syscall_args, 6 * sizeof(unative_t)); |
325 | 335 | ||
326 | _thread_op_end(t); |
336 | _thread_op_end(t); |
327 | interrupts_restore(ipl); |
337 | interrupts_restore(ipl); |
328 | 338 | ||
329 | *buffer = arg_buffer; |
339 | *buffer = arg_buffer; |
330 | return 0; |
340 | return 0; |
331 | } |
341 | } |
332 | 342 | ||
333 | int udebug_regs_read(thread_t *t, void **buffer, size_t *n) |
343 | int udebug_regs_read(thread_t *t, void **buffer, size_t *n) |
334 | { |
344 | { |
335 | istate_t *state; |
345 | istate_t *state; |
336 | void *regs_buffer; |
346 | void *regs_buffer; |
337 | int rc; |
347 | int rc; |
338 | ipl_t ipl; |
348 | ipl_t ipl; |
339 | 349 | ||
340 | klog_printf("udebug_regs_read()"); |
350 | klog_printf("udebug_regs_read()"); |
341 | 351 | ||
- | 352 | /* Prepare a buffer to hold the registers */ |
|
- | 353 | regs_buffer = malloc(sizeof(istate_t), 0); |
|
- | 354 | if (!regs_buffer) return ENOMEM; |
|
- | 355 | ||
342 | ipl = interrupts_disable(); |
356 | ipl = interrupts_disable(); |
343 | 357 | ||
344 | /* On success, this will lock t->debug_lock */ |
358 | /* On success, this will lock t->debug_lock */ |
345 | rc = _thread_op_begin(t); |
359 | rc = _thread_op_begin(t); |
346 | if (rc != EOK) { |
360 | if (rc != EOK) { |
347 | interrupts_restore(ipl); |
361 | interrupts_restore(ipl); |
348 | return rc; |
362 | return rc; |
349 | } |
363 | } |
350 | 364 | ||
351 | state = t->uspace_state; |
365 | state = t->uspace_state; |
352 | if (state == NULL) { |
366 | if (state == NULL) { |
353 | _thread_op_end(t); |
367 | _thread_op_end(t); |
354 | interrupts_restore(ipl); |
368 | interrupts_restore(ipl); |
355 | klog_printf("udebug_regs_read() - istate not available"); |
369 | klog_printf("udebug_regs_read() - istate not available"); |
356 | return EBUSY; |
370 | return EBUSY; |
357 | } |
371 | } |
358 | 372 | ||
359 | /* Copy to an allocated buffer */ |
373 | /* Copy to the allocated buffer */ |
360 | regs_buffer = malloc(sizeof(istate_t), 0); // ??? |
- | |
361 | memcpy(regs_buffer, state, sizeof(istate_t)); |
374 | memcpy(regs_buffer, state, sizeof(istate_t)); |
362 | 375 | ||
363 | _thread_op_end(t); |
376 | _thread_op_end(t); |
364 | interrupts_restore(ipl); |
377 | interrupts_restore(ipl); |
365 | 378 | ||
366 | *buffer = regs_buffer; |
379 | *buffer = regs_buffer; |
367 | *n = sizeof(istate_t); |
380 | *n = sizeof(istate_t); |
368 | 381 | ||
369 | return 0; |
382 | return 0; |
370 | } |
383 | } |
371 | 384 | ||
372 | int udebug_regs_write(thread_t *t, void *buffer) |
385 | int udebug_regs_write(thread_t *t, void *buffer) |
373 | { |
386 | { |
374 | int rc; |
387 | int rc; |
375 | istate_t *state; |
388 | istate_t *state; |
376 | ipl_t ipl; |
389 | ipl_t ipl; |
377 | 390 | ||
378 | klog_printf("udebug_regs_write()"); |
391 | klog_printf("udebug_regs_write()"); |
379 | 392 | ||
380 | /* Try to change the thread's uspace_state */ |
393 | /* Try to change the thread's uspace_state */ |
381 | 394 | ||
382 | ipl = interrupts_disable(); |
395 | ipl = interrupts_disable(); |
383 | 396 | ||
384 | /* On success, this will lock t->debug_lock */ |
397 | /* On success, this will lock t->debug_lock */ |
385 | rc = _thread_op_begin(t); |
398 | rc = _thread_op_begin(t); |
386 | if (rc != EOK) { |
399 | if (rc != EOK) { |
387 | interrupts_restore(ipl); |
400 | interrupts_restore(ipl); |
388 | return rc; |
401 | return rc; |
389 | } |
402 | } |
390 | 403 | ||
391 | state = t->uspace_state; |
404 | state = t->uspace_state; |
392 | if (state == NULL) { |
405 | if (state == NULL) { |
393 | _thread_op_end(t); |
406 | _thread_op_end(t); |
394 | interrupts_restore(ipl); |
407 | interrupts_restore(ipl); |
395 | klog_printf("udebug_regs_write() - istate not available"); |
408 | klog_printf("udebug_regs_write() - istate not available"); |
396 | 409 | ||
397 | return EBUSY; |
410 | return EBUSY; |
398 | } |
411 | } |
399 | 412 | ||
400 | memcpy(t->uspace_state, buffer, sizeof(t->uspace_state)); |
413 | memcpy(t->uspace_state, buffer, sizeof(t->uspace_state)); |
401 | 414 | ||
402 | _thread_op_end(t); |
415 | _thread_op_end(t); |
403 | interrupts_restore(ipl); |
416 | interrupts_restore(ipl); |
404 | 417 | ||
405 | return 0; |
418 | return 0; |
406 | } |
419 | } |
407 | 420 | ||
408 | 421 | ||
409 | int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer) |
422 | int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer) |
410 | { |
423 | { |
411 | void *data_buffer; |
424 | void *data_buffer; |
412 | int rc; |
425 | int rc; |
413 | 426 | ||
414 | klog_printf("udebug_mem_read()"); |
427 | klog_printf("udebug_mem_read()"); |
415 | 428 | ||
416 | data_buffer = malloc(n, 0); // ??? |
429 | data_buffer = malloc(n, 0); |
- | 430 | if (!data_buffer) return ENOMEM; |
|
- | 431 | ||
417 | klog_printf("udebug_mem_read: src=%u, size=%u", uspace_addr, n); |
432 | klog_printf("udebug_mem_read: src=%u, size=%u", uspace_addr, n); |
418 | 433 | ||
419 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
434 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
420 | * be a problem */ |
435 | * be a problem */ |
421 | rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n); |
436 | rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n); |
422 | if (rc) return rc; |
437 | if (rc) return rc; |
423 | 438 | ||
424 | *buffer = data_buffer; |
439 | *buffer = data_buffer; |
425 | return 0; |
440 | return 0; |
426 | } |
441 | } |
427 | 442 | ||
428 | int udebug_mem_write(unative_t uspace_addr, void *data, size_t n) |
443 | int udebug_mem_write(unative_t uspace_addr, void *data, size_t n) |
429 | { |
444 | { |
430 | int rc; |
445 | int rc; |
431 | udebug_task_state_t dts; |
446 | udebug_task_state_t dts; |
432 | 447 | ||
433 | klog_printf("udebug_mem_write()"); |
448 | klog_printf("udebug_mem_write()"); |
434 | 449 | ||
435 | /* Verify task state */ |
450 | /* Verify task state */ |
436 | spinlock_lock(&TASK->lock); |
451 | spinlock_lock(&TASK->lock); |
437 | dts = TASK->dt_state; |
452 | dts = TASK->dt_state; |
438 | spinlock_unlock(&TASK->lock); |
453 | spinlock_unlock(&TASK->lock); |
439 | 454 | ||
440 | if (dts != UDEBUG_TS_ACTIVE) |
455 | if (dts != UDEBUG_TS_ACTIVE) |
441 | return EBUSY; |
456 | return EBUSY; |
442 | 457 | ||
443 | klog_printf("dst=%u, size=%u", uspace_addr, n); |
458 | klog_printf("dst=%u, size=%u", uspace_addr, n); |
444 | 459 | ||
445 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
460 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
446 | * be a problem */ |
461 | * be a problem */ |
447 | rc = copy_to_uspace((void *)uspace_addr, data, n); |
462 | rc = copy_to_uspace((void *)uspace_addr, data, n); |
448 | if (rc) return rc; |
463 | if (rc) return rc; |
449 | 464 | ||
450 | return 0; |
465 | return 0; |
451 | } |
466 | } |
452 | 467 | ||
453 | /** @} |
468 | /** @} |
454 | */ |
469 | */ |
455 | 470 |