e692ada8274edd417b303fd41751869cd920fafd
[openocd.git] / src / rtos / linux.c
1 /***************************************************************************
2 * Copyright (C) 2011 by STEricsson *
3 * Heythem Bouhaja heythem.bouhaja@stericsson.com : creation *
4 * Michel JAOUEN michel.jaouen@stericsson.com : adaptation to rtos *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
20 ***************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <helper/time_support.h>
27 #include <jtag/jtag.h>
28 #include "target/target.h"
29 #include "target/target_type.h"
30 #include "helper/log.h"
31 #include "helper/types.h"
32 #include "rtos.h"
33 #include "rtos_standard_stackings.h"
34 #include <target/register.h>
35 #include "server/gdb_server.h"
36
37 #define LINUX_USER_KERNEL_BORDER 0xc0000000
38 #include "linux_header.h"
39 #define PHYS
40 #define MAX_THREADS 200
41 /* specific task */
42 struct linux_os {
43 char *name;
44 uint32_t init_task_addr;
45 int thread_count;
46 int threadid_count;
47 int preupdtate_threadid_count;
48 int nr_cpus;
49 int threads_lookup;
50 int threads_needs_update;
51 struct current_thread *current_threads;
52 struct threads *thread_list;
53 /* virt2phys parameter */
54 uint32_t phys_mask;
55 uint32_t phys_base;
56 };
57
58 struct current_thread {
59 int64_t threadid;
60 int32_t core_id;
61 #ifdef PID_CHECK
62 uint32_t pid;
63 #endif
64 uint32_t TS;
65 struct current_thread *next;
66 };
67
68 struct threads {
69 char name[17];
70 uint32_t base_addr; /* address to read magic */
71 uint32_t state; /* magic value : filled only at creation */
72 uint32_t pid; /* linux pid : id for identifying a thread */
73 uint32_t oncpu; /* content cpu number in current thread */
74 uint32_t asid; /* filled only at creation */
75 int64_t threadid;
76 int status; /* dead = 1 alive = 2 current = 3 alive and current */
77 /* value that should not change during the live of a thread ? */
78 uint32_t thread_info_addr; /* contain latest thread_info_addr computed */
79 /* retrieve from thread_info */
80 struct cpu_context *context;
81 struct threads *next;
82 };
83
84 struct cpu_context {
85 uint32_t R4;
86 uint32_t R5;
87 uint32_t R6;
88 uint32_t R7;
89 uint32_t R8;
90 uint32_t R9;
91 uint32_t IP;
92 uint32_t FP;
93 uint32_t SP;
94 uint32_t PC;
95 uint32_t preempt_count;
96 };
97 struct cpu_context *cpu_context_read(struct target *target, uint32_t base_addr,
98 uint32_t *info_addr);
99 static int insert_into_threadlist(struct target *target, struct threads *t);
100
101 static int linux_os_create(struct target *target);
102
103 static int linux_os_dummy_update(struct rtos *rtos)
104 {
105 /* update is done only when thread request come
106 * too many thread to do it on each stop */
107 return 0;
108 }
109
110 static int linux_compute_virt2phys(struct target *target, uint32_t address)
111 {
112 struct linux_os *linux_os = (struct linux_os *)
113 target->rtos->rtos_specific_params;
114 uint32_t pa = 0;
115 int retval = target->type->virt2phys(target, address, &pa);
116 if (retval != ERROR_OK) {
117 LOG_ERROR("Cannot compute linux virt2phys translation");
118 /* fixes default address */
119 linux_os->phys_base = 0;
120 return ERROR_FAIL;
121 }
122
123 linux_os->init_task_addr = address;
124 address = address & linux_os->phys_mask;
125 linux_os->phys_base = pa - address;
126 return ERROR_OK;
127 }
128
129 static int linux_read_memory(struct target *target,
130 uint32_t address, uint32_t size, uint32_t count,
131 uint8_t *buffer)
132 {
133 #ifdef PHYS
134 struct linux_os *linux_os = (struct linux_os *)
135 target->rtos->rtos_specific_params;
136 uint32_t pa = (address & linux_os->phys_mask) + linux_os->phys_base;
137 #endif
138 if (address < 0xc000000) {
139 LOG_ERROR("linux awareness : address in user space");
140 return ERROR_FAIL;
141 }
142 #ifdef PHYS
143 target_read_phys_memory(target, pa, size, count, buffer);
144 #endif
145 target_read_memory(target, address, size, count, buffer);
146 return ERROR_OK;
147 }
148
149 static char *reg_converter(char *buffer, void *reg, int size)
150 {
151 int i;
152
153 for (i = 0; i < size; i++)
154 buffer += sprintf(buffer, "%02x", ((uint8_t *) reg)[i]);
155
156 return buffer;
157 }
158
159 int fill_buffer(struct target *target, uint32_t addr, uint8_t *buffer)
160 {
161
162 if ((addr & 0xfffffffc) != addr)
163 LOG_INFO("unaligned address %x!!", addr);
164
165 int retval = linux_read_memory(target, addr, 4, 1, buffer);
166 return retval;
167
168 }
169
170 uint32_t get_buffer(struct target *target, const uint8_t *buffer)
171 {
172 uint32_t value = 0;
173 const uint8_t *value_ptr = buffer;
174 value = target_buffer_get_u32(target, value_ptr);
175 return value;
176 }
177
178 static int linux_os_thread_reg_list(struct rtos *rtos,
179 int64_t thread_id, char **hex_reg_list)
180 {
181 struct target *target = rtos->target;
182 struct linux_os *linux_os = (struct linux_os *)
183 target->rtos->rtos_specific_params;
184 int i = 0;
185 struct current_thread *tmp = linux_os->current_threads;
186 struct current_thread *next;
187 char *hex_string;
188 int found = 0;
189 int retval;
190 /* check if a current thread is requested */
191 next = tmp;
192
193 do {
194 if (next->threadid == thread_id)
195 found = 1;
196 else
197 next = next->next;
198 } while ((found == 0) && (next != tmp) && (next != NULL));
199
200 if (found == 1) {
201 /* search target to perfom the access */
202 struct reg **reg_list;
203 int reg_list_size, reg_packet_size = 0;
204 struct target_list *head;
205 head = target->head;
206 found = 0;
207 do {
208 if (head->target->coreid == next->core_id) {
209
210 target = head->target;
211 found = 1;
212 } else
213 head = head->next;
214
215 } while ((head != (struct target_list *)NULL) && (found == 0));
216
217 if (found == 0) {
218 LOG_ERROR
219 (
220 "current thread %" PRIx64 ": no target to perform access of core id %x",
221 thread_id,
222 next->core_id);
223 return ERROR_FAIL;
224 }
225
226 /*LOG_INFO("thread %lx current on core %x",thread_id,
227 * target->coreid);*/
228 retval =
229 target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
230 REG_CLASS_GENERAL);
231
232 if (retval != ERROR_OK)
233 return retval;
234
235 for (i = 0; i < reg_list_size; i++)
236 reg_packet_size += reg_list[i]->size;
237
238 assert(reg_packet_size > 0);
239
240 *hex_reg_list = malloc(DIV_ROUND_UP(reg_packet_size, 8) * 2);
241
242 hex_string = *hex_reg_list;
243
244 for (i = 0; i < reg_list_size; i++) {
245 if (!reg_list[i]->valid)
246 reg_list[i]->type->get(reg_list[i]);
247
248 hex_string = reg_converter(hex_string,
249 reg_list[i]->value,
250 (reg_list[i]->size) / 8);
251 }
252
253 free(reg_list);
254
255 } else {
256 struct threads *temp = linux_os->thread_list;
257 *hex_reg_list = (char *)calloc(1, 500 * sizeof(char));
258 hex_string = *hex_reg_list;
259
260 for (i = 0; i < 16; i++)
261 hex_string += sprintf(hex_string, "%02x", 0);
262
263 while ((temp != NULL) &&
264 (temp->threadid != target->rtos->current_threadid))
265 temp = temp->next;
266
267 if (temp != NULL) {
268 if (temp->context == NULL)
269 temp->context = cpu_context_read(target,
270 temp->
271 base_addr,
272 &temp->
273 thread_info_addr);
274
275 hex_string =
276 reg_converter(hex_string, &temp->context->R4, 4);
277 hex_string =
278 reg_converter(hex_string, &temp->context->R5, 4);
279 hex_string =
280 reg_converter(hex_string, &temp->context->R6, 4);
281 hex_string =
282 reg_converter(hex_string, &temp->context->R7, 4);
283 hex_string =
284 reg_converter(hex_string, &temp->context->R8, 4);
285 hex_string =
286 reg_converter(hex_string, &temp->context->R9, 4);
287
288 for (i = 0; i < 4; i++) /*R10 = 0x0 */
289 hex_string += sprintf(hex_string, "%02x", 0);
290
291 hex_string =
292 reg_converter(hex_string, &temp->context->FP, 4);
293 hex_string =
294 reg_converter(hex_string, &temp->context->IP, 4);
295 hex_string =
296 reg_converter(hex_string, &temp->context->SP, 4);
297
298 for (i = 0; i < 4; i++)
299 hex_string += sprintf(hex_string, "%02x", 0);
300
301 hex_string =
302 reg_converter(hex_string, &temp->context->PC, 4);
303
304 for (i = 0; i < 100; i++) /*100 */
305 hex_string += sprintf(hex_string, "%02x", 0);
306
307 uint32_t cpsr = 0x00000000;
308 reg_converter(hex_string, &cpsr, 4);
309 }
310 }
311 return ERROR_OK;
312 }
313
314 static int linux_os_detect(struct target *target)
315 {
316 LOG_INFO("should no be called");
317 return 0;
318 }
319
320 static int linux_os_smp_init(struct target *target);
321 static int linux_os_clean(struct target *target);
322 #define INIT_TASK 0
323 static char *linux_symbol_list[] = {
324 "init_task",
325 NULL
326 };
327
328 static int linux_get_symbol_list_to_lookup(symbol_table_elem_t *symbol_list[])
329 {
330 unsigned int i;
331 *symbol_list = (symbol_table_elem_t *)
332 malloc(sizeof(symbol_table_elem_t) * ARRAY_SIZE(linux_symbol_list));
333
334 for (i = 0; i < ARRAY_SIZE(linux_symbol_list); i++)
335 (*symbol_list)[i].symbol_name = linux_symbol_list[i];
336
337 return 0;
338 }
339
340 static char *linux_ps_command(struct target *target);
341
342 const struct rtos_type Linux_os = {
343 .name = "linux",
344 .detect_rtos = linux_os_detect,
345 .create = linux_os_create,
346 .smp_init = linux_os_smp_init,
347 .update_threads = linux_os_dummy_update,
348 .get_thread_reg_list = linux_os_thread_reg_list,
349 .get_symbol_list_to_lookup = linux_get_symbol_list_to_lookup,
350 .clean = linux_os_clean,
351 .ps_command = linux_ps_command,
352 };
353
354 static int linux_thread_packet(struct connection *connection, char *packet,
355 int packet_size);
356 static void linux_identify_current_threads(struct target *target);
357
358 #ifdef PID_CHECK
359 int fill_task_pid(struct target *target, struct threads *t)
360 {
361 uint32_t pid_addr = t->base_addr + PID;
362 uint8_t buffer[4];
363 int retval = fill_buffer(target, pid_addr, buffer);
364
365 if (retval == ERROR_OK) {
366 uint32_t val = get_buffer(target, buffer);
367 t->pid = val;
368 } else
369 LOG_ERROR("fill_task_pid: unable to read memory");
370
371 return retval;
372 }
373 #endif
374
375 int fill_task(struct target *target, struct threads *t)
376 {
377 int retval;
378 uint32_t pid_addr = t->base_addr + PID;
379 uint32_t mem_addr = t->base_addr + MEM;
380 uint32_t on_cpu = t->base_addr + ONCPU;
381 uint8_t *buffer = calloc(1, 4);
382 retval = fill_buffer(target, t->base_addr, buffer);
383
384 if (retval == ERROR_OK) {
385 uint32_t val = get_buffer(target, buffer);
386 t->state = val;
387 } else
388 LOG_ERROR("fill_task: unable to read memory");
389
390 retval = fill_buffer(target, pid_addr, buffer);
391
392 if (retval == ERROR_OK) {
393 uint32_t val = get_buffer(target, buffer);
394 t->pid = val;
395 } else
396 LOG_ERROR("fill task: unable to read memory");
397
398 retval = fill_buffer(target, on_cpu, buffer);
399
400 if (retval == ERROR_OK) {
401 uint32_t val = get_buffer(target, buffer);
402 t->oncpu = val;
403 } else
404 LOG_ERROR("fill task: unable to read memory");
405
406 retval = fill_buffer(target, mem_addr, buffer);
407
408 if (retval == ERROR_OK) {
409 uint32_t val = get_buffer(target, buffer);
410
411 if (val != 0) {
412 uint32_t asid_addr = val + MM_CTX;
413 retval = fill_buffer(target, asid_addr, buffer);
414
415 if (retval == ERROR_OK) {
416 val = get_buffer(target, buffer);
417 t->asid = val;
418 } else
419 LOG_ERROR
420 ("fill task: unable to read memory -- ASID");
421 } else
422 t->asid = 0;
423 } else
424 LOG_ERROR("fill task: unable to read memory");
425
426 free(buffer);
427
428 return retval;
429 }
430
431 int get_name(struct target *target, struct threads *t)
432 {
433 int retval;
434 uint32_t full_name[4];
435 uint32_t comm = t->base_addr + COMM;
436 int i;
437
438 for (i = 0; i < 17; i++)
439 t->name[i] = 0;
440
441 retval = linux_read_memory(target, comm, 4, 4, (uint8_t *) full_name);
442
443 if (retval != ERROR_OK) {
444 LOG_ERROR("get_name: unable to read memory\n");
445 return ERROR_FAIL;
446 }
447
448 uint32_t raw_name = target_buffer_get_u32(target,
449 (const uint8_t *)
450 &full_name[0]);
451 t->name[3] = raw_name >> 24;
452 t->name[2] = raw_name >> 16;
453 t->name[1] = raw_name >> 8;
454 t->name[0] = raw_name;
455 raw_name =
456 target_buffer_get_u32(target, (const uint8_t *)&full_name[1]);
457 t->name[7] = raw_name >> 24;
458 t->name[6] = raw_name >> 16;
459 t->name[5] = raw_name >> 8;
460 t->name[4] = raw_name;
461 raw_name =
462 target_buffer_get_u32(target, (const uint8_t *)&full_name[2]);
463 t->name[11] = raw_name >> 24;
464 t->name[10] = raw_name >> 16;
465 t->name[9] = raw_name >> 8;
466 t->name[8] = raw_name;
467 raw_name =
468 target_buffer_get_u32(target, (const uint8_t *)&full_name[3]);
469 t->name[15] = raw_name >> 24;
470 t->name[14] = raw_name >> 16;
471 t->name[13] = raw_name >> 8;
472 t->name[12] = raw_name;
473 return ERROR_OK;
474
475 }
476
477 int get_current(struct target *target, int create)
478 {
479 struct target_list *head;
480 head = target->head;
481 uint8_t *buf;
482 uint32_t val;
483 uint32_t ti_addr;
484 uint8_t *buffer = calloc(1, 4);
485 struct linux_os *linux_os = (struct linux_os *)
486 target->rtos->rtos_specific_params;
487 struct current_thread *ctt = linux_os->current_threads;
488
489 /* invalid current threads content */
490 while (ctt != NULL) {
491 ctt->threadid = -1;
492 ctt->TS = 0xdeadbeef;
493 ctt = ctt->next;
494 }
495
496 while (head != (struct target_list *)NULL) {
497 struct reg **reg_list;
498 int reg_list_size;
499 int retval;
500
501 if (target_get_gdb_reg_list(head->target, &reg_list,
502 &reg_list_size, REG_CLASS_GENERAL) != ERROR_OK) {
503 free(buffer);
504 return ERROR_TARGET_FAILURE;
505 }
506
507 if (!reg_list[13]->valid)
508 reg_list[13]->type->get(reg_list[13]);
509
510 buf = reg_list[13]->value;
511 val = get_buffer(target, buf);
512 ti_addr = (val & 0xffffe000);
513 uint32_t TS_addr = ti_addr + 0xc;
514 retval = fill_buffer(target, TS_addr, buffer);
515
516 if (retval == ERROR_OK) {
517 uint32_t TS = get_buffer(target, buffer);
518 uint32_t cpu, on_cpu = TS + ONCPU;
519 retval = fill_buffer(target, on_cpu, buffer);
520
521 if (retval == ERROR_OK) {
522 /*uint32_t cpu = get_buffer(target, buffer);*/
523 struct current_thread *ct =
524 linux_os->current_threads;
525 cpu = head->target->coreid;
526
527 while ((ct != NULL) && (ct->core_id != (int32_t) cpu))
528 ct = ct->next;
529
530 if ((ct != NULL) && (ct->TS == 0xdeadbeef))
531 ct->TS = TS;
532 else
533 LOG_ERROR
534 ("error in linux current thread update");
535
536 if (create && ct) {
537 struct threads *t;
538 t = calloc(1, sizeof(struct threads));
539 t->base_addr = ct->TS;
540 fill_task(target, t);
541 get_name(target, t);
542 t->oncpu = cpu;
543 insert_into_threadlist(target, t);
544 t->status = 3;
545 t->thread_info_addr = 0xdeadbeef;
546 ct->threadid = t->threadid;
547 linux_os->thread_count++;
548 #ifdef PID_CHECK
549 ct->pid = t->pid;
550 #endif
551 /*LOG_INFO("Creation of current thread %s",t->name);*/
552 }
553 }
554 }
555
556 free(reg_list);
557 head = head->next;
558 }
559
560 free(buffer);
561
562 return ERROR_OK;
563 }
564
565 struct cpu_context *cpu_context_read(struct target *target, uint32_t base_addr,
566 uint32_t *thread_info_addr_old)
567 {
568 struct cpu_context *context = calloc(1, sizeof(struct cpu_context));
569 uint32_t preempt_count_addr = 0;
570 uint32_t registers[10];
571 uint8_t *buffer = calloc(1, 4);
572 uint32_t stack = base_addr + QAT;
573 uint32_t thread_info_addr = 0;
574 uint32_t thread_info_addr_update = 0;
575 int retval = ERROR_FAIL;
576 context->R4 = 0xdeadbeef;
577 context->R5 = 0xdeadbeef;
578 context->R6 = 0xdeadbeef;
579 context->R7 = 0xdeadbeef;
580 context->R8 = 0xdeadbeef;
581 context->R9 = 0xdeadbeef;
582 context->IP = 0xdeadbeef;
583 context->FP = 0xdeadbeef;
584 context->SP = 0xdeadbeef;
585 context->PC = 0xdeadbeef;
586 retry:
587
588 if (*thread_info_addr_old == 0xdeadbeef) {
589 retval = fill_buffer(target, stack, buffer);
590
591 if (retval == ERROR_OK)
592 thread_info_addr = get_buffer(target, buffer);
593 else
594 LOG_ERROR("cpu_context: unable to read memory");
595
596 thread_info_addr_update = thread_info_addr;
597 } else
598 thread_info_addr = *thread_info_addr_old;
599
600 preempt_count_addr = thread_info_addr + PREEMPT;
601 retval = fill_buffer(target, preempt_count_addr, buffer);
602
603 if (retval == ERROR_OK)
604 context->preempt_count = get_buffer(target, buffer);
605 else {
606 if (*thread_info_addr_old != 0xdeadbeef) {
607 LOG_ERROR
608 ("cpu_context: cannot read at thread_info_addr");
609
610 if (*thread_info_addr_old < LINUX_USER_KERNEL_BORDER)
611 LOG_INFO
612 ("cpu_context : thread_info_addr in userspace!!!");
613
614 *thread_info_addr_old = 0xdeadbeef;
615 goto retry;
616 }
617
618 LOG_ERROR("cpu_context: unable to read memory");
619 }
620
621 thread_info_addr += CPU_CONT;
622
623 retval = linux_read_memory(target, thread_info_addr, 4, 10,
624 (uint8_t *) registers);
625
626 if (retval != ERROR_OK) {
627 free(buffer);
628 LOG_ERROR("cpu_context: unable to read memory\n");
629 return context;
630 }
631
632 context->R4 =
633 target_buffer_get_u32(target, (const uint8_t *)&registers[0]);
634 context->R5 =
635 target_buffer_get_u32(target, (const uint8_t *)&registers[1]);
636 context->R6 =
637 target_buffer_get_u32(target, (const uint8_t *)&registers[2]);
638 context->R7 =
639 target_buffer_get_u32(target, (const uint8_t *)&registers[3]);
640 context->R8 =
641 target_buffer_get_u32(target, (const uint8_t *)&registers[4]);
642 context->R9 =
643 target_buffer_get_u32(target, (const uint8_t *)&registers[5]);
644 context->IP =
645 target_buffer_get_u32(target, (const uint8_t *)&registers[6]);
646 context->FP =
647 target_buffer_get_u32(target, (const uint8_t *)&registers[7]);
648 context->SP =
649 target_buffer_get_u32(target, (const uint8_t *)&registers[8]);
650 context->PC =
651 target_buffer_get_u32(target, (const uint8_t *)&registers[9]);
652
653 if (*thread_info_addr_old == 0xdeadbeef)
654 *thread_info_addr_old = thread_info_addr_update;
655
656 free(buffer);
657
658 return context;
659 }
660
661 uint32_t next_task(struct target *target, struct threads *t)
662 {
663 uint8_t *buffer = calloc(1, 4);
664 uint32_t next_addr = t->base_addr + NEXT;
665 int retval = fill_buffer(target, next_addr, buffer);
666
667 if (retval == ERROR_OK) {
668 uint32_t val = get_buffer(target, buffer);
669 val = val - NEXT;
670 free(buffer);
671 return val;
672 } else
673 LOG_ERROR("next task: unable to read memory");
674
675 free(buffer);
676
677 return 0;
678 }
679
680 struct current_thread *add_current_thread(struct current_thread *currents,
681 struct current_thread *ct)
682 {
683 ct->next = NULL;
684
685 if (currents == NULL) {
686 currents = ct;
687 return currents;
688 } else {
689 struct current_thread *temp = currents;
690
691 while (temp->next != NULL)
692 temp = temp->next;
693
694 temp->next = ct;
695 return currents;
696 }
697 }
698
699 struct threads *liste_del_task(struct threads *task_list, struct threads **t,
700 struct threads *prev)
701 {
702 LOG_INFO("del task %" PRId64, (*t)->threadid);
703 prev->next = (*t)->next;
704
705 if (prev == task_list)
706 task_list = prev;
707
708 /* free content of threads */
709 if ((*t)->context)
710 free((*t)->context);
711
712 free(*t);
713 *t = prev;
714 return task_list;
715 }
716
717 struct threads *liste_add_task(struct threads *task_list, struct threads *t,
718 struct threads **last)
719 {
720 t->next = NULL;
721
722 if (*last == NULL)
723 if (task_list == NULL) {
724 task_list = t;
725 return task_list;
726 } else {
727 struct threads *temp = task_list;
728
729 while (temp->next != NULL)
730 temp = temp->next;
731
732 temp->next = t;
733 *last = t;
734 return task_list;
735 } else {
736 (*last)->next = t;
737 *last = t;
738 return task_list;
739 }
740 }
741
742 #ifdef PID_CHECK
743 static int current_pid(struct linux_os *linux_os, uint32_t pid)
744 #else
745 static int current_base_addr(struct linux_os *linux_os, uint32_t base_addr)
746 #endif
747 {
748 struct current_thread *ct = linux_os->current_threads;
749 #ifdef PID_CHECK
750
751 while ((ct != NULL) && (ct->pid != pid))
752 #else
753 while ((ct != NULL) && (ct->TS != base_addr))
754 #endif
755 ct = ct->next;
756 #ifdef PID_CHECK
757 if ((ct != NULL) && (ct->pid == pid))
758 #else
759 if ((ct != NULL) && (ct->TS == base_addr))
760 #endif
761 return 1;
762
763 return 0;
764 }
765
766 int linux_get_tasks(struct target *target, int context)
767 {
768 int loop = 0;
769 int retval = 0;
770 struct linux_os *linux_os = (struct linux_os *)
771 target->rtos->rtos_specific_params;
772 linux_os->thread_list = NULL;
773 linux_os->thread_count = 0;
774
775 if (linux_os->init_task_addr == 0xdeadbeef) {
776 LOG_INFO("no init symbol\n");
777 return ERROR_FAIL;
778 }
779
780 int64_t start = timeval_ms();
781
782 struct threads *t = calloc(1, sizeof(struct threads));
783 struct threads *last = NULL;
784 t->base_addr = linux_os->init_task_addr;
785 /* retrieve the thread id , currently running in the different smp core */
786 get_current(target, 1);
787
788 while (((t->base_addr != linux_os->init_task_addr) &&
789 (t->base_addr != 0)) || (loop == 0)) {
790 loop++;
791 fill_task(target, t);
792 retval = get_name(target, t);
793
794 if (loop > MAX_THREADS) {
795 free(t);
796 LOG_INFO("more than %d threads !!", MAX_THREADS);
797 return ERROR_FAIL;
798 }
799
800 if (retval != ERROR_OK) {
801 free(t);
802 return ERROR_FAIL;
803 }
804
805 /* check that this thread is not one the current threads already
806 * created */
807 #ifdef PID_CHECK
808
809 if (!current_pid(linux_os, t->pid)) {
810 #else
811 if (!current_base_addr(linux_os, t->base_addr)) {
812 #endif
813 t->threadid = linux_os->threadid_count;
814 t->status = 1;
815 linux_os->threadid_count++;
816
817 linux_os->thread_list =
818 liste_add_task(linux_os->thread_list, t, &last);
819 /* no interest to fill the context if it is a current thread. */
820 linux_os->thread_count++;
821 t->thread_info_addr = 0xdeadbeef;
822
823 if (context)
824 t->context =
825 cpu_context_read(target, t->base_addr,
826 &t->thread_info_addr);
827 } else {
828 /*LOG_INFO("thread %s is a current thread already created",t->name); */
829 free(t);
830 }
831
832 uint32_t base_addr = next_task(target, t);
833 t = calloc(1, sizeof(struct threads));
834 t->base_addr = base_addr;
835 }
836
837 linux_os->threads_lookup = 1;
838 linux_os->threads_needs_update = 0;
839 linux_os->preupdtate_threadid_count = linux_os->threadid_count - 1;
840 /* check that all current threads have been identified */
841
842 LOG_INFO("complete time %" PRId64 ", thread mean %" PRId64 "\n",
843 (timeval_ms() - start),
844 (timeval_ms() - start) / linux_os->threadid_count);
845
846 LOG_INFO("threadid count %d", linux_os->threadid_count);
847 free(t);
848
849 return ERROR_OK;
850 }
851
852 static int clean_threadlist(struct target *target)
853 {
854 struct linux_os *linux_os = (struct linux_os *)
855 target->rtos->rtos_specific_params;
856 struct threads *old, *temp = linux_os->thread_list;
857
858 while (temp != NULL) {
859 old = temp;
860
861 if (temp->context)
862 free(temp->context);
863
864 temp = temp->next;
865 free(old);
866 }
867
868 return ERROR_OK;
869 }
870
871 static int linux_os_clean(struct target *target)
872 {
873 struct linux_os *os_linux = (struct linux_os *)
874 target->rtos->rtos_specific_params;
875 clean_threadlist(target);
876 os_linux->init_task_addr = 0xdeadbeef;
877 os_linux->name = "linux";
878 os_linux->thread_list = NULL;
879 os_linux->thread_count = 0;
880 os_linux->nr_cpus = 0;
881 os_linux->threads_lookup = 0;
882 os_linux->threads_needs_update = 0;
883 os_linux->threadid_count = 1;
884 return ERROR_OK;
885 }
886
887 static int insert_into_threadlist(struct target *target, struct threads *t)
888 {
889 struct linux_os *linux_os = (struct linux_os *)
890 target->rtos->rtos_specific_params;
891 struct threads *temp = linux_os->thread_list;
892 t->threadid = linux_os->threadid_count;
893 linux_os->threadid_count++;
894 t->status = 1;
895 t->next = NULL;
896
897 if (temp == NULL)
898 linux_os->thread_list = t;
899 else {
900 while (temp->next != NULL)
901 temp = temp->next;
902
903 t->next = NULL;
904 temp->next = t;
905 }
906
907 return ERROR_OK;
908 }
909
910 static void linux_identify_current_threads(struct target *target)
911 {
912 struct linux_os *linux_os = (struct linux_os *)
913 target->rtos->rtos_specific_params;
914 struct threads *thread_list = linux_os->thread_list;
915 struct current_thread *ct = linux_os->current_threads;
916 struct threads *t = NULL;
917
918 while ((ct != NULL)) {
919 if (ct->threadid == -1) {
920
921 /* un-identified thread */
922 int found = 0;
923 t = calloc(1, sizeof(struct threads));
924 t->base_addr = ct->TS;
925 #ifdef PID_CHECK
926
927 if (fill_task_pid(target, t) != ERROR_OK) {
928 error_handling:
929 free(t);
930 LOG_ERROR
931 ("linux identify_current_threads: unable to read pid");
932 return;
933 }
934 #endif
935
936 /* search in the list of threads if pid
937 already present */
938 while ((thread_list != NULL) && (found == 0)) {
939 #ifdef PID_CHECK
940 if (thread_list->pid == t->pid) {
941 #else
942 if (thread_list->base_addr == t->base_addr) {
943 #endif
944 free(t);
945 t = thread_list;
946 found = 1;
947 }
948 thread_list = thread_list->next;
949 }
950
951 if (!found) {
952 /* it is a new thread */
953 if (fill_task(target, t) != ERROR_OK)
954 goto error_handling;
955
956 get_name(target, t);
957 insert_into_threadlist(target, t);
958 t->thread_info_addr = 0xdeadbeef;
959 }
960
961 t->status = 3;
962 ct->threadid = t->threadid;
963 #ifdef PID_CHECK
964 ct->pid = t->pid;
965 #endif
966 linux_os->thread_count++;
967 #if 0
968 if (found == 0)
969 LOG_INFO("current thread core %x identified %s",
970 ct->core_id, t->name);
971 else
972 LOG_INFO("current thread core %x, reused %s",
973 ct->core_id, t->name);
974 #endif
975 }
976 #if 0
977 else {
978 struct threads tmp;
979 tmp.base_addr = ct->TS;
980 get_name(target, &tmp);
981 LOG_INFO("current thread core %x , already identified %s !!!",
982 ct->core_id, tmp.name);
983 }
984 #endif
985 ct = ct->next;
986 }
987
988 return;
989 #ifndef PID_CHECK
990 error_handling:
991 free(t);
992 LOG_ERROR("unable to read pid");
993 return;
994
995 #endif
996 }
997
998 static int linux_task_update(struct target *target, int context)
999 {
1000 struct linux_os *linux_os = (struct linux_os *)
1001 target->rtos->rtos_specific_params;
1002 struct threads *thread_list = linux_os->thread_list;
1003 int retval;
1004 int loop = 0;
1005 linux_os->thread_count = 0;
1006
1007 /*thread_list = thread_list->next; skip init_task*/
1008 while (thread_list != NULL) {
1009 thread_list->status = 0; /*setting all tasks to dead state*/
1010
1011 if (thread_list->context) {
1012 free(thread_list->context);
1013 thread_list->context = NULL;
1014 }
1015
1016 thread_list = thread_list->next;
1017 }
1018
1019 int found = 0;
1020
1021 if (linux_os->init_task_addr == 0xdeadbeef) {
1022 LOG_INFO("no init symbol\n");
1023 return ERROR_FAIL;
1024 }
1025 int64_t start = timeval_ms();
1026 struct threads *t = calloc(1, sizeof(struct threads));
1027 uint32_t previous = 0xdeadbeef;
1028 t->base_addr = linux_os->init_task_addr;
1029 retval = get_current(target, 0);
1030 /*check that all current threads have been identified */
1031 linux_identify_current_threads(target);
1032
1033 while (((t->base_addr != linux_os->init_task_addr) &&
1034 (t->base_addr != previous)) || (loop == 0)) {
1035 /* for avoiding any permanent loop for any reason possibly due to
1036 * target */
1037 loop++;
1038 previous = t->base_addr;
1039 /* read only pid */
1040 #ifdef PID_CHECK
1041 retval = fill_task_pid(target, t);
1042 #endif
1043
1044 if (retval != ERROR_OK) {
1045 free(t);
1046 return ERROR_FAIL;
1047 }
1048
1049 thread_list = linux_os->thread_list;
1050
1051 while (thread_list != NULL) {
1052 #ifdef PID_CHECK
1053 if (t->pid == thread_list->pid) {
1054 #else
1055 if (t->base_addr == thread_list->base_addr) {
1056 #endif
1057 if (!thread_list->status) {
1058 #ifdef PID_CHECK
1059 if (t->base_addr != thread_list->base_addr)
1060 LOG_INFO("thread base_addr has changed !!");
1061 #endif
1062 /* this is not a current thread */
1063 thread_list->base_addr = t->base_addr;
1064 thread_list->status = 1;
1065
1066 /* we don 't update this field any more */
1067
1068 /*thread_list->state = t->state;
1069 thread_list->oncpu = t->oncpu;
1070 thread_list->asid = t->asid;
1071 */
1072 if (context)
1073 thread_list->context =
1074 cpu_context_read(target,
1075 thread_list->
1076 base_addr,
1077 &thread_list->
1078 thread_info_addr);
1079 } else {
1080 /* it is a current thread no need to read context */
1081 }
1082
1083 linux_os->thread_count++;
1084 found = 1;
1085 break;
1086 } else {
1087 found = 0;
1088 thread_list = thread_list->next;
1089 }
1090 }
1091
1092 if (found == 0) {
1093 uint32_t base_addr;
1094 fill_task(target, t);
1095 get_name(target, t);
1096 retval = insert_into_threadlist(target, t);
1097 t->thread_info_addr = 0xdeadbeef;
1098
1099 if (context)
1100 t->context =
1101 cpu_context_read(target, t->base_addr,
1102 &t->thread_info_addr);
1103
1104 base_addr = next_task(target, t);
1105 t = calloc(1, sizeof(struct threads));
1106 t->base_addr = base_addr;
1107 linux_os->thread_count++;
1108 } else
1109 t->base_addr = next_task(target, t);
1110 }
1111
1112 LOG_INFO("update thread done %" PRId64 ", mean%" PRId64 "\n",
1113 (timeval_ms() - start), (timeval_ms() - start) / loop);
1114 free(t);
1115 linux_os->threads_needs_update = 0;
1116 return ERROR_OK;
1117 }
1118
1119 int linux_gdb_thread_packet(struct target *target,
1120 struct connection *connection, char *packet,
1121 int packet_size)
1122 {
1123 int retval;
1124 struct linux_os *linux_os =
1125 (struct linux_os *)target->rtos->rtos_specific_params;
1126
1127 if (linux_os->init_task_addr == 0xdeadbeef) {
1128 /* it has not been initialized */
1129 LOG_INFO("received thread request without init task address");
1130 gdb_put_packet(connection, "l", 1);
1131 return ERROR_OK;
1132 }
1133
1134 retval = linux_get_tasks(target, 1);
1135
1136 if (retval != ERROR_OK)
1137 return ERROR_TARGET_FAILURE;
1138
1139 char *out_str = (char *)calloc(1, 350 * sizeof(int64_t));
1140 char *tmp_str = out_str;
1141 tmp_str += sprintf(tmp_str, "m");
1142 struct threads *temp = linux_os->thread_list;
1143
1144 while (temp != NULL) {
1145 tmp_str += sprintf(tmp_str, "%016" PRIx64, temp->threadid);
1146 temp = temp->next;
1147 if (temp)
1148 tmp_str += sprintf(tmp_str, ",");
1149 }
1150
1151 gdb_put_packet(connection, out_str, strlen(out_str));
1152 return ERROR_OK;
1153 }
1154
1155 int linux_gdb_thread_update(struct target *target,
1156 struct connection *connection, char *packet,
1157 int packet_size)
1158 {
1159 int found = 0;
1160 struct linux_os *linux_os = (struct linux_os *)
1161 target->rtos->rtos_specific_params;
1162 struct threads *temp = linux_os->thread_list;
1163
1164 while (temp != NULL) {
1165 if (temp->threadid == linux_os->preupdtate_threadid_count + 1) {
1166 /*LOG_INFO("FOUND");*/
1167 found = 1;
1168 break;
1169 } else
1170 temp = temp->next;
1171 }
1172
1173 if (found == 1) {
1174 /*LOG_INFO("INTO GDB THREAD UPDATE FOUNDING START TASK");*/
1175 char *out_strr = (char *)calloc(1, 350 * sizeof(int64_t));
1176 char *tmp_strr = out_strr;
1177 tmp_strr += sprintf(tmp_strr, "m");
1178 /*LOG_INFO("CHAR MALLOC & M DONE");*/
1179 tmp_strr += sprintf(tmp_strr, "%016" PRIx64, temp->threadid);
1180
1181 temp = temp->next;
1182
1183 while (temp != NULL) {
1184 /*LOG_INFO("INTO GDB THREAD UPDATE WHILE");*/
1185 tmp_strr += sprintf(tmp_strr, ",");
1186 tmp_strr +=
1187 sprintf(tmp_strr, "%016" PRIx64, temp->threadid);
1188 temp = temp->next;
1189 }
1190
1191 /*tmp_str[0] = 0;*/
1192 gdb_put_packet(connection, out_strr, strlen(out_strr));
1193 linux_os->preupdtate_threadid_count =
1194 linux_os->threadid_count - 1;
1195 free(out_strr);
1196 } else
1197 gdb_put_packet(connection, "l", 1);
1198
1199 return ERROR_OK;
1200 }
1201
1202 int linux_thread_extra_info(struct target *target,
1203 struct connection *connection, char *packet,
1204 int packet_size)
1205 {
1206 int64_t threadid = 0;
1207 struct linux_os *linux_os = (struct linux_os *)
1208 target->rtos->rtos_specific_params;
1209 sscanf(packet, "qThreadExtraInfo,%" SCNx64, &threadid);
1210 /*LOG_INFO("lookup extra info for thread %" SCNx64, threadid);*/
1211 struct threads *temp = linux_os->thread_list;
1212
1213 while (temp != NULL) {
1214 if (temp->threadid == threadid) {
1215 char *pid = " PID: ";
1216 char *pid_current = "*PID: ";
1217 char *name = "NAME: ";
1218 int str_size = strlen(pid) + strlen(name);
1219 char *tmp_str = (char *)calloc(1, str_size + 50);
1220 char *tmp_str_ptr = tmp_str;
1221
1222 /* discriminate current task */
1223 if (temp->status == 3)
1224 tmp_str_ptr += sprintf(tmp_str_ptr, "%s",
1225 pid_current);
1226 else
1227 tmp_str_ptr += sprintf(tmp_str_ptr, "%s", pid);
1228
1229 tmp_str_ptr +=
1230 sprintf(tmp_str_ptr, "%d", (int)temp->pid);
1231 tmp_str_ptr += sprintf(tmp_str_ptr, "%s", " | ");
1232 sprintf(tmp_str_ptr, "%s", name);
1233 sprintf(tmp_str_ptr, "%s", temp->name);
1234 char *hex_str = (char *)calloc(1, strlen(tmp_str) * 2 + 1);
1235 int pkt_len = hexify(hex_str, tmp_str, 0, strlen(tmp_str) * 2 + 1);
1236 gdb_put_packet(connection, hex_str, pkt_len);
1237 free(hex_str);
1238 free(tmp_str);
1239 return ERROR_OK;
1240 }
1241
1242 temp = temp->next;
1243 }
1244
1245 LOG_INFO("thread not found");
1246 return ERROR_OK;
1247 }
1248
1249 int linux_gdb_T_packet(struct connection *connection,
1250 struct target *target, char *packet, int packet_size)
1251 {
1252 int64_t threadid;
1253 struct linux_os *linux_os = (struct linux_os *)
1254 target->rtos->rtos_specific_params;
1255 int retval = ERROR_OK;
1256 sscanf(packet, "T%" SCNx64, &threadid);
1257
1258 if (linux_os->threads_needs_update == 0) {
1259 struct threads *temp = linux_os->thread_list;
1260 struct threads *prev = linux_os->thread_list;
1261
1262 while (temp != NULL) {
1263 if (temp->threadid == threadid) {
1264 if (temp->status != 0) {
1265 gdb_put_packet(connection, "OK", 2);
1266 return ERROR_OK;
1267 } else {
1268 /* delete item in the list */
1269 linux_os->thread_list =
1270 liste_del_task(linux_os->
1271 thread_list, &temp,
1272 prev);
1273 linux_os->thread_count--;
1274 gdb_put_packet(connection, "E01", 3);
1275 return ERROR_OK;
1276 }
1277 }
1278
1279 /* for deletion */
1280 prev = temp;
1281 temp = temp->next;
1282 }
1283
1284 LOG_INFO("gdb requested status on non existing thread");
1285 gdb_put_packet(connection, "E01", 3);
1286 return ERROR_OK;
1287
1288 } else {
1289 retval = linux_task_update(target, 1);
1290 struct threads *temp = linux_os->thread_list;
1291
1292 while (temp != NULL) {
1293 if (temp->threadid == threadid) {
1294 if (temp->status == 1) {
1295 gdb_put_packet(connection, "OK", 2);
1296 return ERROR_OK;
1297 } else {
1298 gdb_put_packet(connection, "E01", 3);
1299 return ERROR_OK;
1300 }
1301 }
1302
1303 temp = temp->next;
1304 }
1305 }
1306
1307 return retval;
1308 }
1309
1310 int linux_gdb_h_packet(struct connection *connection,
1311 struct target *target, char *packet, int packet_size)
1312 {
1313 struct linux_os *linux_os = (struct linux_os *)
1314 target->rtos->rtos_specific_params;
1315 struct current_thread *ct = linux_os->current_threads;
1316
1317 /* select to display the current thread of the selected target */
1318 while ((ct != NULL) && (ct->core_id != target->coreid))
1319 ct = ct->next;
1320
1321 int64_t current_gdb_thread_rq;
1322
1323 if (linux_os->threads_lookup == 1) {
1324 if ((ct != NULL) && (ct->threadid == -1)) {
1325 ct = linux_os->current_threads;
1326
1327 while ((ct != NULL) && (ct->threadid == -1))
1328 ct = ct->next;
1329 }
1330
1331 if (ct == NULL) {
1332 /* no current thread can be identified
1333 * any way with smp */
1334 LOG_INFO("no current thread identified");
1335 /* attempt to display the name of the 2 threads identified with
1336 * get_current */
1337 struct threads t;
1338 ct = linux_os->current_threads;
1339
1340 while ((ct != NULL) && (ct->threadid == -1)) {
1341 t.base_addr = ct->TS;
1342 get_name(target, &t);
1343 LOG_INFO("name of unidentified thread %s",
1344 t.name);
1345 ct = ct->next;
1346 }
1347
1348 gdb_put_packet(connection, "OK", 2);
1349 return ERROR_OK;
1350 }
1351
1352 if (packet[1] == 'g') {
1353 sscanf(packet, "Hg%16" SCNx64, &current_gdb_thread_rq);
1354
1355 if (current_gdb_thread_rq == 0) {
1356 target->rtos->current_threadid = ct->threadid;
1357 gdb_put_packet(connection, "OK", 2);
1358 } else {
1359 target->rtos->current_threadid =
1360 current_gdb_thread_rq;
1361 gdb_put_packet(connection, "OK", 2);
1362 }
1363 } else if (packet[1] == 'c') {
1364 sscanf(packet, "Hc%16" SCNx64, &current_gdb_thread_rq);
1365
1366 if ((current_gdb_thread_rq == 0) ||
1367 (current_gdb_thread_rq == ct->threadid)) {
1368 target->rtos->current_threadid = ct->threadid;
1369 gdb_put_packet(connection, "OK", 2);
1370 } else
1371 gdb_put_packet(connection, "E01", 3);
1372 }
1373 } else
1374 gdb_put_packet(connection, "OK", 2);
1375
1376 return ERROR_OK;
1377 }
1378
1379 static int linux_thread_packet(struct connection *connection, char *packet,
1380 int packet_size)
1381 {
1382 int retval = ERROR_OK;
1383 struct current_thread *ct;
1384 struct target *target = get_target_from_connection(connection);
1385 struct linux_os *linux_os = (struct linux_os *)
1386 target->rtos->rtos_specific_params;
1387
1388 switch (packet[0]) {
1389 case 'T': /* Is thread alive?*/
1390
1391 linux_gdb_T_packet(connection, target, packet, packet_size);
1392 break;
1393 case 'H': /* Set current thread */
1394 /* ( 'c' for step and continue, 'g' for all other operations )*/
1395 /*LOG_INFO(" H packet received '%s'", packet);*/
1396 linux_gdb_h_packet(connection, target, packet, packet_size);
1397 break;
1398 case 'q':
1399
1400 if (strncmp(packet, "qSymbol", 7) == 0) {
1401 if (rtos_qsymbol(connection, packet, packet_size) == 1) {
1402 linux_compute_virt2phys(target,
1403 target->rtos->
1404 symbols[INIT_TASK].
1405 address);
1406 }
1407
1408 break;
1409 } else if (strncmp(packet, "qfThreadInfo", 12) == 0) {
1410 if (linux_os->thread_list == NULL) {
1411 retval = linux_gdb_thread_packet(target,
1412 connection,
1413 packet,
1414 packet_size);
1415 break;
1416 } else {
1417 retval = linux_gdb_thread_update(target,
1418 connection,
1419 packet,
1420 packet_size);
1421 break;
1422 }
1423 } else if (strncmp(packet, "qsThreadInfo", 12) == 0) {
1424 gdb_put_packet(connection, "l", 1);
1425 break;
1426 } else if (strncmp(packet, "qThreadExtraInfo,", 17) == 0) {
1427 linux_thread_extra_info(target, connection, packet,
1428 packet_size);
1429 break;
1430 } else {
1431 retval = GDB_THREAD_PACKET_NOT_CONSUMED;
1432 break;
1433 }
1434
1435 case 'Q':
1436 /* previously response was : thread not found
1437 * gdb_put_packet(connection, "E01", 3); */
1438 retval = GDB_THREAD_PACKET_NOT_CONSUMED;
1439 break;
1440 case 'c':
1441 case 's': {
1442 if (linux_os->threads_lookup == 1) {
1443 ct = linux_os->current_threads;
1444
1445 while ((ct != NULL) && (ct->core_id) != target->coreid)
1446 ct = ct->next;
1447
1448 if ((ct != NULL) && (ct->threadid == -1)) {
1449 ct = linux_os->current_threads;
1450
1451 while ((ct != NULL) && (ct->threadid == -1))
1452 ct = ct->next;
1453 }
1454
1455 if ((ct != NULL) && (ct->threadid !=
1456 target->rtos->
1457 current_threadid)
1458 && (target->rtos->current_threadid != -1))
1459 LOG_WARNING("WARNING! current GDB thread do not match" \
1460 "current thread running." \
1461 "Switch thread in GDB to threadid %d",
1462 (int)ct->threadid);
1463
1464 LOG_INFO("threads_needs_update = 1");
1465 linux_os->threads_needs_update = 1;
1466 }
1467 }
1468
1469 /* if a packet handler returned an error, exit input loop */
1470 if (retval != ERROR_OK)
1471 return retval;
1472 }
1473
1474 return retval;
1475 }
1476
1477 static int linux_os_smp_init(struct target *target)
1478 {
1479 struct target_list *head;
1480 /* keep only target->rtos */
1481 struct rtos *rtos = target->rtos;
1482 struct linux_os *os_linux =
1483 (struct linux_os *)rtos->rtos_specific_params;
1484 struct current_thread *ct;
1485 head = target->head;
1486
1487 while (head != (struct target_list *)NULL) {
1488 if (head->target->rtos != rtos) {
1489 struct linux_os *smp_os_linux =
1490 (struct linux_os *)head->target->rtos->
1491 rtos_specific_params;
1492 /* remap smp target on rtos */
1493 free(head->target->rtos);
1494 head->target->rtos = rtos;
1495 /* reuse allocated ct */
1496 ct = smp_os_linux->current_threads;
1497 ct->threadid = -1;
1498 ct->TS = 0xdeadbeef;
1499 ct->core_id = head->target->coreid;
1500 os_linux->current_threads =
1501 add_current_thread(os_linux->current_threads, ct);
1502 os_linux->nr_cpus++;
1503 free(smp_os_linux);
1504 }
1505
1506 head = head->next;
1507 }
1508
1509 return ERROR_OK;
1510 }
1511
1512 static int linux_os_create(struct target *target)
1513 {
1514 struct linux_os *os_linux = calloc(1, sizeof(struct linux_os));
1515 struct current_thread *ct = calloc(1, sizeof(struct current_thread));
1516 LOG_INFO("linux os creation\n");
1517 os_linux->init_task_addr = 0xdeadbeef;
1518 os_linux->name = "linux";
1519 os_linux->thread_list = NULL;
1520 os_linux->thread_count = 0;
1521 target->rtos->current_threadid = -1;
1522 os_linux->nr_cpus = 1;
1523 os_linux->threads_lookup = 0;
1524 os_linux->threads_needs_update = 0;
1525 os_linux->threadid_count = 1;
1526 os_linux->current_threads = NULL;
1527 target->rtos->rtos_specific_params = (void *)os_linux;
1528 ct->core_id = target->coreid;
1529 ct->threadid = -1;
1530 ct->TS = 0xdeadbeef;
1531 os_linux->current_threads =
1532 add_current_thread(os_linux->current_threads, ct);
1533 /* overload rtos thread default handler */
1534 target->rtos->gdb_thread_packet = linux_thread_packet;
1535 /* initialize a default virt 2 phys translation */
1536 os_linux->phys_mask = ~0xc0000000;
1537 os_linux->phys_base = 0x0;
1538 return JIM_OK;
1539 }
1540
1541 static char *linux_ps_command(struct target *target)
1542 {
1543 struct linux_os *linux_os = (struct linux_os *)
1544 target->rtos->rtos_specific_params;
1545 int retval = ERROR_OK;
1546 char *display;
1547
1548 if (linux_os->threads_lookup == 0)
1549 retval = linux_get_tasks(target, 1);
1550 else {
1551 if (linux_os->threads_needs_update != 0)
1552 retval = linux_task_update(target, 0);
1553 }
1554
1555 if (retval == ERROR_OK) {
1556 struct threads *temp = linux_os->thread_list;
1557 char *tmp;
1558 LOG_INFO("allocation for %d threads line",
1559 linux_os->thread_count);
1560 display = calloc((linux_os->thread_count + 2) * 80, 1);
1561
1562 if (!display)
1563 goto error;
1564
1565 tmp = display;
1566 tmp += sprintf(tmp, "PID\t\tCPU\t\tASID\t\tNAME\n");
1567 tmp += sprintf(tmp, "---\t\t---\t\t----\t\t----\n");
1568
1569 while (temp != NULL) {
1570 if (temp->status) {
1571 if (temp->context)
1572 tmp +=
1573 sprintf(tmp,
1574 "%d\t\t%d\t\t%x\t\t%s\n",
1575 (int)temp->pid, temp->oncpu,
1576 temp->asid, temp->name);
1577 else
1578 tmp +=
1579 sprintf(tmp,
1580 "%d\t\t%d\t\t%x\t\t%s\n",
1581 (int)temp->pid, temp->oncpu,
1582 temp->asid, temp->name);
1583 }
1584
1585 temp = temp->next;
1586 }
1587
1588 return display;
1589 }
1590
1591 error:
1592 display = calloc(40, 1);
1593 sprintf(display, "linux_ps_command failed\n");
1594 return display;
1595 }