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 *
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. *
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. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
24 #include <helper/time_support.h>
25 #include <jtag/jtag.h>
26 #include "target/target.h"
27 #include "target/target_type.h"
28 #include "helper/log.h"
29 #include "helper/types.h"
31 #include "rtos_standard_stackings.h"
32 #include <target/register.h>
33 #include "server/gdb_server.h"
35 #define LINUX_USER_KERNEL_BORDER 0xc0000000
36 #include "linux_header.h"
38 #define MAX_THREADS 200
42 uint32_t init_task_addr
;
45 int preupdtate_threadid_count
;
48 int threads_needs_update
;
49 struct current_thread
*current_threads
;
50 struct threads
*thread_list
;
51 /* virt2phys parameter */
56 struct current_thread
{
63 struct current_thread
*next
;
68 uint32_t base_addr
; /* address to read magic */
69 uint32_t state
; /* magic value : filled only at creation */
70 uint32_t pid
; /* linux pid : id for identifying a thread */
71 uint32_t oncpu
; /* content cpu number in current thread */
72 uint32_t asid
; /* filled only at creation */
74 int status
; /* dead = 1 alive = 2 current = 3 alive and current */
75 /* value that should not change during the live of a thread ? */
76 uint32_t thread_info_addr
; /* contain latest thread_info_addr computed */
77 /* retrieve from thread_info */
78 struct cpu_context
*context
;
93 uint32_t preempt_count
;
95 static struct cpu_context
*cpu_context_read(struct target
*target
, uint32_t base_addr
,
97 static int insert_into_threadlist(struct target
*target
, struct threads
*t
);
99 static int linux_os_create(struct target
*target
);
101 static int linux_os_dummy_update(struct rtos
*rtos
)
103 /* update is done only when thread request come
104 * too many thread to do it on each stop */
108 static int linux_compute_virt2phys(struct target
*target
, target_addr_t address
)
110 struct linux_os
*linux_os
= (struct linux_os
*)
111 target
->rtos
->rtos_specific_params
;
112 target_addr_t pa
= 0;
113 int retval
= target
->type
->virt2phys(target
, address
, &pa
);
114 if (retval
!= ERROR_OK
) {
115 LOG_ERROR("Cannot compute linux virt2phys translation");
116 /* fixes default address */
117 linux_os
->phys_base
= 0;
121 linux_os
->init_task_addr
= address
;
122 address
= address
& linux_os
->phys_mask
;
123 linux_os
->phys_base
= pa
- address
;
127 static int linux_read_memory(struct target
*target
,
128 uint32_t address
, uint32_t size
, uint32_t count
,
132 struct linux_os
*linux_os
= (struct linux_os
*)
133 target
->rtos
->rtos_specific_params
;
134 uint32_t pa
= (address
& linux_os
->phys_mask
) + linux_os
->phys_base
;
136 if (address
< 0xc000000) {
137 LOG_ERROR("linux awareness : address in user space");
141 target_read_phys_memory(target
, pa
, size
, count
, buffer
);
143 target_read_memory(target
, address
, size
, count
, buffer
);
147 static int fill_buffer(struct target
*target
, uint32_t addr
, uint8_t *buffer
)
150 if ((addr
& 0xfffffffc) != addr
)
151 LOG_INFO("unaligned address %" PRIx32
"!!", addr
);
153 int retval
= linux_read_memory(target
, addr
, 4, 1, buffer
);
158 static uint32_t get_buffer(struct target
*target
, const uint8_t *buffer
)
161 const uint8_t *value_ptr
= buffer
;
162 value
= target_buffer_get_u32(target
, value_ptr
);
166 static int linux_os_thread_reg_list(struct rtos
*rtos
,
167 int64_t thread_id
, struct rtos_reg
**reg_list
, int *num_regs
)
169 struct target
*target
= rtos
->target
;
170 struct linux_os
*linux_os
= (struct linux_os
*)
171 target
->rtos
->rtos_specific_params
;
172 struct current_thread
*tmp
= linux_os
->current_threads
;
173 struct current_thread
*next
;
176 /* check if a current thread is requested */
180 if (next
->threadid
== thread_id
)
184 } while ((found
== 0) && (next
!= tmp
) && (next
));
187 LOG_ERROR("could not find thread: %" PRIx64
, thread_id
);
191 /* search target to perform the access */
192 struct reg
**gdb_reg_list
;
193 struct target_list
*head
;
197 if (head
->target
->coreid
== next
->core_id
) {
198 target
= head
->target
;
208 "current thread %" PRIx64
": no target to perform access of core id %" PRIx32
,
214 /*LOG_INFO("thread %lx current on core %x",thread_id, target->coreid);*/
215 retval
= target_get_gdb_reg_list(target
, &gdb_reg_list
, num_regs
, REG_CLASS_GENERAL
);
216 if (retval
!= ERROR_OK
)
219 *reg_list
= calloc(*num_regs
, sizeof(struct rtos_reg
));
221 for (int i
= 0; i
< *num_regs
; ++i
) {
222 if (!gdb_reg_list
[i
]->valid
)
223 gdb_reg_list
[i
]->type
->get(gdb_reg_list
[i
]);
225 (*reg_list
)[i
].number
= gdb_reg_list
[i
]->number
;
226 (*reg_list
)[i
].size
= gdb_reg_list
[i
]->size
;
228 buf_cpy(gdb_reg_list
[i
]->value
, (*reg_list
)[i
].value
, (*reg_list
)[i
].size
);
234 static bool linux_os_detect(struct target
*target
)
236 LOG_INFO("should no be called");
240 static int linux_os_smp_init(struct target
*target
);
241 static int linux_os_clean(struct target
*target
);
243 static const char * const linux_symbol_list
[] = {
248 static int linux_get_symbol_list_to_lookup(struct symbol_table_elem
*symbol_list
[])
251 *symbol_list
= (struct symbol_table_elem
*)
252 calloc(ARRAY_SIZE(linux_symbol_list
), sizeof(struct symbol_table_elem
));
254 for (i
= 0; i
< ARRAY_SIZE(linux_symbol_list
); i
++)
255 (*symbol_list
)[i
].symbol_name
= linux_symbol_list
[i
];
260 static char *linux_ps_command(struct target
*target
);
262 const struct rtos_type linux_rtos
= {
264 .detect_rtos
= linux_os_detect
,
265 .create
= linux_os_create
,
266 .smp_init
= linux_os_smp_init
,
267 .update_threads
= linux_os_dummy_update
,
268 .get_thread_reg_list
= linux_os_thread_reg_list
,
269 .get_symbol_list_to_lookup
= linux_get_symbol_list_to_lookup
,
270 .clean
= linux_os_clean
,
271 .ps_command
= linux_ps_command
,
274 static int linux_thread_packet(struct connection
*connection
, char const *packet
,
276 static void linux_identify_current_threads(struct target
*target
);
279 int fill_task_pid(struct target
*target
, struct threads
*t
)
281 uint32_t pid_addr
= t
->base_addr
+ PID
;
283 int retval
= fill_buffer(target
, pid_addr
, buffer
);
285 if (retval
== ERROR_OK
) {
286 uint32_t val
= get_buffer(target
, buffer
);
289 LOG_ERROR("fill_task_pid: unable to read memory");
295 static int fill_task(struct target
*target
, struct threads
*t
)
298 uint32_t pid_addr
= t
->base_addr
+ PID
;
299 uint32_t mem_addr
= t
->base_addr
+ MEM
;
300 uint32_t on_cpu
= t
->base_addr
+ ONCPU
;
301 uint8_t *buffer
= calloc(1, 4);
302 retval
= fill_buffer(target
, t
->base_addr
, buffer
);
304 if (retval
== ERROR_OK
) {
305 uint32_t val
= get_buffer(target
, buffer
);
308 LOG_ERROR("fill_task: unable to read memory");
310 retval
= fill_buffer(target
, pid_addr
, buffer
);
312 if (retval
== ERROR_OK
) {
313 uint32_t val
= get_buffer(target
, buffer
);
316 LOG_ERROR("fill task: unable to read memory");
318 retval
= fill_buffer(target
, on_cpu
, buffer
);
320 if (retval
== ERROR_OK
) {
321 uint32_t val
= get_buffer(target
, buffer
);
324 LOG_ERROR("fill task: unable to read memory");
326 retval
= fill_buffer(target
, mem_addr
, buffer
);
328 if (retval
== ERROR_OK
) {
329 uint32_t val
= get_buffer(target
, buffer
);
332 uint32_t asid_addr
= val
+ MM_CTX
;
333 retval
= fill_buffer(target
, asid_addr
, buffer
);
335 if (retval
== ERROR_OK
) {
336 val
= get_buffer(target
, buffer
);
340 ("fill task: unable to read memory -- ASID");
344 LOG_ERROR("fill task: unable to read memory");
351 static int get_name(struct target
*target
, struct threads
*t
)
354 uint32_t full_name
[4];
355 uint32_t comm
= t
->base_addr
+ COMM
;
358 for (i
= 0; i
< 17; i
++)
361 retval
= linux_read_memory(target
, comm
, 4, 4, (uint8_t *) full_name
);
363 if (retval
!= ERROR_OK
) {
364 LOG_ERROR("get_name: unable to read memory\n");
368 uint32_t raw_name
= target_buffer_get_u32(target
,
371 t
->name
[3] = raw_name
>> 24;
372 t
->name
[2] = raw_name
>> 16;
373 t
->name
[1] = raw_name
>> 8;
374 t
->name
[0] = raw_name
;
376 target_buffer_get_u32(target
, (const uint8_t *)&full_name
[1]);
377 t
->name
[7] = raw_name
>> 24;
378 t
->name
[6] = raw_name
>> 16;
379 t
->name
[5] = raw_name
>> 8;
380 t
->name
[4] = raw_name
;
382 target_buffer_get_u32(target
, (const uint8_t *)&full_name
[2]);
383 t
->name
[11] = raw_name
>> 24;
384 t
->name
[10] = raw_name
>> 16;
385 t
->name
[9] = raw_name
>> 8;
386 t
->name
[8] = raw_name
;
388 target_buffer_get_u32(target
, (const uint8_t *)&full_name
[3]);
389 t
->name
[15] = raw_name
>> 24;
390 t
->name
[14] = raw_name
>> 16;
391 t
->name
[13] = raw_name
>> 8;
392 t
->name
[12] = raw_name
;
397 static int get_current(struct target
*target
, int create
)
399 struct target_list
*head
;
404 uint8_t *buffer
= calloc(1, 4);
405 struct linux_os
*linux_os
= (struct linux_os
*)
406 target
->rtos
->rtos_specific_params
;
407 struct current_thread
*ctt
= linux_os
->current_threads
;
409 /* invalid current threads content */
412 ctt
->TS
= 0xdeadbeef;
417 struct reg
**reg_list
;
421 if (target_get_gdb_reg_list(head
->target
, ®_list
,
422 ®_list_size
, REG_CLASS_GENERAL
) != ERROR_OK
) {
424 return ERROR_TARGET_FAILURE
;
427 if (!reg_list
[13]->valid
)
428 reg_list
[13]->type
->get(reg_list
[13]);
430 buf
= reg_list
[13]->value
;
431 val
= get_buffer(target
, buf
);
432 ti_addr
= (val
& 0xffffe000);
433 uint32_t ts_addr
= ti_addr
+ 0xc;
434 retval
= fill_buffer(target
, ts_addr
, buffer
);
436 if (retval
== ERROR_OK
) {
437 uint32_t TS
= get_buffer(target
, buffer
);
438 uint32_t cpu
, on_cpu
= TS
+ ONCPU
;
439 retval
= fill_buffer(target
, on_cpu
, buffer
);
441 if (retval
== ERROR_OK
) {
442 /*uint32_t cpu = get_buffer(target, buffer);*/
443 struct current_thread
*ct
=
444 linux_os
->current_threads
;
445 cpu
= head
->target
->coreid
;
447 while ((ct
) && (ct
->core_id
!= (int32_t) cpu
))
450 if ((ct
) && (ct
->TS
== 0xdeadbeef))
454 ("error in linux current thread update");
458 t
= calloc(1, sizeof(struct threads
));
459 t
->base_addr
= ct
->TS
;
460 fill_task(target
, t
);
463 insert_into_threadlist(target
, t
);
465 t
->thread_info_addr
= 0xdeadbeef;
466 ct
->threadid
= t
->threadid
;
467 linux_os
->thread_count
++;
471 /*LOG_INFO("Creation of current thread %s",t->name);*/
485 static struct cpu_context
*cpu_context_read(struct target
*target
, uint32_t base_addr
,
486 uint32_t *thread_info_addr_old
)
488 struct cpu_context
*context
= calloc(1, sizeof(struct cpu_context
));
489 uint32_t preempt_count_addr
= 0;
490 uint32_t registers
[10];
491 uint8_t *buffer
= calloc(1, 4);
492 uint32_t stack
= base_addr
+ QAT
;
493 uint32_t thread_info_addr
= 0;
494 uint32_t thread_info_addr_update
= 0;
495 int retval
= ERROR_FAIL
;
496 context
->R4
= 0xdeadbeef;
497 context
->R5
= 0xdeadbeef;
498 context
->R6
= 0xdeadbeef;
499 context
->R7
= 0xdeadbeef;
500 context
->R8
= 0xdeadbeef;
501 context
->R9
= 0xdeadbeef;
502 context
->IP
= 0xdeadbeef;
503 context
->FP
= 0xdeadbeef;
504 context
->SP
= 0xdeadbeef;
505 context
->PC
= 0xdeadbeef;
508 if (*thread_info_addr_old
== 0xdeadbeef) {
509 retval
= fill_buffer(target
, stack
, buffer
);
511 if (retval
== ERROR_OK
)
512 thread_info_addr
= get_buffer(target
, buffer
);
514 LOG_ERROR("cpu_context: unable to read memory");
516 thread_info_addr_update
= thread_info_addr
;
518 thread_info_addr
= *thread_info_addr_old
;
520 preempt_count_addr
= thread_info_addr
+ PREEMPT
;
521 retval
= fill_buffer(target
, preempt_count_addr
, buffer
);
523 if (retval
== ERROR_OK
)
524 context
->preempt_count
= get_buffer(target
, buffer
);
526 if (*thread_info_addr_old
!= 0xdeadbeef) {
528 ("cpu_context: cannot read at thread_info_addr");
530 if (*thread_info_addr_old
< LINUX_USER_KERNEL_BORDER
)
532 ("cpu_context : thread_info_addr in userspace!!!");
534 *thread_info_addr_old
= 0xdeadbeef;
538 LOG_ERROR("cpu_context: unable to read memory");
541 thread_info_addr
+= CPU_CONT
;
543 retval
= linux_read_memory(target
, thread_info_addr
, 4, 10,
544 (uint8_t *) registers
);
546 if (retval
!= ERROR_OK
) {
548 LOG_ERROR("cpu_context: unable to read memory\n");
553 target_buffer_get_u32(target
, (const uint8_t *)®isters
[0]);
555 target_buffer_get_u32(target
, (const uint8_t *)®isters
[1]);
557 target_buffer_get_u32(target
, (const uint8_t *)®isters
[2]);
559 target_buffer_get_u32(target
, (const uint8_t *)®isters
[3]);
561 target_buffer_get_u32(target
, (const uint8_t *)®isters
[4]);
563 target_buffer_get_u32(target
, (const uint8_t *)®isters
[5]);
565 target_buffer_get_u32(target
, (const uint8_t *)®isters
[6]);
567 target_buffer_get_u32(target
, (const uint8_t *)®isters
[7]);
569 target_buffer_get_u32(target
, (const uint8_t *)®isters
[8]);
571 target_buffer_get_u32(target
, (const uint8_t *)®isters
[9]);
573 if (*thread_info_addr_old
== 0xdeadbeef)
574 *thread_info_addr_old
= thread_info_addr_update
;
581 static uint32_t next_task(struct target
*target
, struct threads
*t
)
583 uint8_t *buffer
= calloc(1, 4);
584 uint32_t next_addr
= t
->base_addr
+ NEXT
;
585 int retval
= fill_buffer(target
, next_addr
, buffer
);
587 if (retval
== ERROR_OK
) {
588 uint32_t val
= get_buffer(target
, buffer
);
593 LOG_ERROR("next task: unable to read memory");
600 static struct current_thread
*add_current_thread(struct current_thread
*currents
,
601 struct current_thread
*ct
)
609 struct current_thread
*temp
= currents
;
619 static struct threads
*liste_del_task(struct threads
*task_list
, struct threads
**t
,
620 struct threads
*prev
)
622 LOG_INFO("del task %" PRId64
, (*t
)->threadid
);
624 prev
->next
= (*t
)->next
;
626 task_list
= (*t
)->next
;
628 /* free content of threads */
632 *t
= prev
? prev
: task_list
;
636 static struct threads
*liste_add_task(struct threads
*task_list
, struct threads
*t
,
637 struct threads
**last
)
646 struct threads
*temp
= task_list
;
662 static int current_pid(struct linux_os
*linux_os
, uint32_t pid
)
664 static int current_base_addr(struct linux_os
*linux_os
, uint32_t base_addr
)
667 struct current_thread
*ct
= linux_os
->current_threads
;
670 while ((ct
) && (ct
->pid
!= pid
))
672 while ((ct
) && (ct
->TS
!= base_addr
))
676 if ((ct
) && (ct
->pid
== pid
))
678 if ((ct
) && (ct
->TS
== base_addr
))
685 static int linux_get_tasks(struct target
*target
, int context
)
689 struct linux_os
*linux_os
= (struct linux_os
*)
690 target
->rtos
->rtos_specific_params
;
691 linux_os
->thread_list
= NULL
;
692 linux_os
->thread_count
= 0;
694 if (linux_os
->init_task_addr
== 0xdeadbeef) {
695 LOG_INFO("no init symbol\n");
699 int64_t start
= timeval_ms();
701 struct threads
*t
= calloc(1, sizeof(struct threads
));
702 struct threads
*last
= NULL
;
703 t
->base_addr
= linux_os
->init_task_addr
;
704 /* retrieve the thread id , currently running in the different smp core */
705 get_current(target
, 1);
707 while (((t
->base_addr
!= linux_os
->init_task_addr
) &&
708 (t
->base_addr
!= 0)) || (loop
== 0)) {
710 fill_task(target
, t
);
711 retval
= get_name(target
, t
);
713 if (loop
> MAX_THREADS
) {
715 LOG_INFO("more than %d threads !!", MAX_THREADS
);
719 if (retval
!= ERROR_OK
) {
724 /* check that this thread is not one the current threads already
729 if (!current_pid(linux_os
, t
->pid
)) {
731 if (!current_base_addr(linux_os
, t
->base_addr
)) {
733 t
->threadid
= linux_os
->threadid_count
;
735 linux_os
->threadid_count
++;
737 linux_os
->thread_list
=
738 liste_add_task(linux_os
->thread_list
, t
, &last
);
739 /* no interest to fill the context if it is a current thread. */
740 linux_os
->thread_count
++;
741 t
->thread_info_addr
= 0xdeadbeef;
745 cpu_context_read(target
, t
->base_addr
,
746 &t
->thread_info_addr
);
747 base_addr
= next_task(target
, t
);
749 /*LOG_INFO("thread %s is a current thread already created",t->name); */
750 base_addr
= next_task(target
, t
);
754 t
= calloc(1, sizeof(struct threads
));
755 t
->base_addr
= base_addr
;
758 linux_os
->threads_lookup
= 1;
759 linux_os
->threads_needs_update
= 0;
760 linux_os
->preupdtate_threadid_count
= linux_os
->threadid_count
- 1;
761 /* check that all current threads have been identified */
763 LOG_INFO("complete time %" PRId64
", thread mean %" PRId64
"\n",
764 (timeval_ms() - start
),
765 (timeval_ms() - start
) / linux_os
->threadid_count
);
767 LOG_INFO("threadid count %d", linux_os
->threadid_count
);
773 static int clean_threadlist(struct target
*target
)
775 struct linux_os
*linux_os
= (struct linux_os
*)
776 target
->rtos
->rtos_specific_params
;
777 struct threads
*old
, *temp
= linux_os
->thread_list
;
791 static int linux_os_clean(struct target
*target
)
793 struct linux_os
*os_linux
= (struct linux_os
*)
794 target
->rtos
->rtos_specific_params
;
795 clean_threadlist(target
);
796 os_linux
->init_task_addr
= 0xdeadbeef;
797 os_linux
->name
= "linux";
798 os_linux
->thread_list
= NULL
;
799 os_linux
->thread_count
= 0;
800 os_linux
->nr_cpus
= 0;
801 os_linux
->threads_lookup
= 0;
802 os_linux
->threads_needs_update
= 0;
803 os_linux
->threadid_count
= 1;
807 static int insert_into_threadlist(struct target
*target
, struct threads
*t
)
809 struct linux_os
*linux_os
= (struct linux_os
*)
810 target
->rtos
->rtos_specific_params
;
811 struct threads
*temp
= linux_os
->thread_list
;
812 t
->threadid
= linux_os
->threadid_count
;
813 linux_os
->threadid_count
++;
818 linux_os
->thread_list
= t
;
830 static void linux_identify_current_threads(struct target
*target
)
832 struct linux_os
*linux_os
= (struct linux_os
*)
833 target
->rtos
->rtos_specific_params
;
834 struct threads
*thread_list
= linux_os
->thread_list
;
835 struct current_thread
*ct
= linux_os
->current_threads
;
836 struct threads
*t
= NULL
;
839 if (ct
->threadid
== -1) {
841 /* un-identified thread */
843 t
= calloc(1, sizeof(struct threads
));
844 t
->base_addr
= ct
->TS
;
847 if (fill_task_pid(target
, t
) != ERROR_OK
) {
851 ("linux identify_current_threads: unable to read pid");
856 /* search in the list of threads if pid
858 while ((thread_list
) && (found
== 0)) {
860 if (thread_list
->pid
== t
->pid
) {
862 if (thread_list
->base_addr
== t
->base_addr
) {
868 thread_list
= thread_list
->next
;
872 /* it is a new thread */
873 if (fill_task(target
, t
) != ERROR_OK
)
877 insert_into_threadlist(target
, t
);
878 t
->thread_info_addr
= 0xdeadbeef;
882 ct
->threadid
= t
->threadid
;
886 linux_os
->thread_count
++;
889 LOG_INFO("current thread core %x identified %s",
890 ct
->core_id
, t
->name
);
892 LOG_INFO("current thread core %x, reused %s",
893 ct
->core_id
, t
->name
);
899 tmp
.base_addr
= ct
->TS
;
900 get_name(target
, &tmp
);
901 LOG_INFO("current thread core %x , already identified %s !!!",
902 ct
->core_id
, tmp
.name
);
912 LOG_ERROR("unable to read pid");
918 static int linux_task_update(struct target
*target
, int context
)
920 struct linux_os
*linux_os
= (struct linux_os
*)
921 target
->rtos
->rtos_specific_params
;
922 struct threads
*thread_list
= linux_os
->thread_list
;
925 linux_os
->thread_count
= 0;
927 /*thread_list = thread_list->next; skip init_task*/
928 while (thread_list
) {
929 thread_list
->status
= 0; /*setting all tasks to dead state*/
931 free(thread_list
->context
);
932 thread_list
->context
= NULL
;
934 thread_list
= thread_list
->next
;
939 if (linux_os
->init_task_addr
== 0xdeadbeef) {
940 LOG_INFO("no init symbol\n");
943 int64_t start
= timeval_ms();
944 struct threads
*t
= calloc(1, sizeof(struct threads
));
945 uint32_t previous
= 0xdeadbeef;
946 t
->base_addr
= linux_os
->init_task_addr
;
947 retval
= get_current(target
, 0);
948 /*check that all current threads have been identified */
949 linux_identify_current_threads(target
);
951 while (((t
->base_addr
!= linux_os
->init_task_addr
) &&
952 (t
->base_addr
!= previous
)) || (loop
== 0)) {
953 /* for avoiding any permanent loop for any reason possibly due to
956 previous
= t
->base_addr
;
959 retval
= fill_task_pid(target
, t
);
962 if (retval
!= ERROR_OK
) {
967 thread_list
= linux_os
->thread_list
;
969 while (thread_list
) {
971 if (t
->pid
== thread_list
->pid
) {
973 if (t
->base_addr
== thread_list
->base_addr
) {
975 if (!thread_list
->status
) {
977 if (t
->base_addr
!= thread_list
->base_addr
)
978 LOG_INFO("thread base_addr has changed !!");
980 /* this is not a current thread */
981 thread_list
->base_addr
= t
->base_addr
;
982 thread_list
->status
= 1;
984 /* we don 't update this field any more */
986 /*thread_list->state = t->state;
987 thread_list->oncpu = t->oncpu;
988 thread_list->asid = t->asid;
991 thread_list
->context
=
992 cpu_context_read(target
,
993 thread_list
->base_addr
,
994 &thread_list
->thread_info_addr
);
996 /* it is a current thread no need to read context */
999 linux_os
->thread_count
++;
1004 thread_list
= thread_list
->next
;
1010 fill_task(target
, t
);
1011 get_name(target
, t
);
1012 retval
= insert_into_threadlist(target
, t
);
1013 t
->thread_info_addr
= 0xdeadbeef;
1017 cpu_context_read(target
, t
->base_addr
,
1018 &t
->thread_info_addr
);
1020 base_addr
= next_task(target
, t
);
1021 t
= calloc(1, sizeof(struct threads
));
1022 t
->base_addr
= base_addr
;
1023 linux_os
->thread_count
++;
1025 t
->base_addr
= next_task(target
, t
);
1028 LOG_INFO("update thread done %" PRId64
", mean%" PRId64
"\n",
1029 (timeval_ms() - start
), (timeval_ms() - start
) / loop
);
1031 linux_os
->threads_needs_update
= 0;
1035 static int linux_gdb_thread_packet(struct target
*target
,
1036 struct connection
*connection
, char const *packet
,
1040 struct linux_os
*linux_os
=
1041 (struct linux_os
*)target
->rtos
->rtos_specific_params
;
1043 if (linux_os
->init_task_addr
== 0xdeadbeef) {
1044 /* it has not been initialized */
1045 LOG_INFO("received thread request without init task address");
1046 gdb_put_packet(connection
, "l", 1);
1050 retval
= linux_get_tasks(target
, 1);
1052 if (retval
!= ERROR_OK
)
1053 return ERROR_TARGET_FAILURE
;
1055 char *out_str
= calloc(MAX_THREADS
* 17 + 10, 1);
1056 char *tmp_str
= out_str
;
1057 tmp_str
+= sprintf(tmp_str
, "m");
1058 struct threads
*temp
= linux_os
->thread_list
;
1061 tmp_str
+= sprintf(tmp_str
, "%016" PRIx64
, temp
->threadid
);
1064 tmp_str
+= sprintf(tmp_str
, ",");
1067 gdb_put_packet(connection
, out_str
, strlen(out_str
));
1072 static int linux_gdb_thread_update(struct target
*target
,
1073 struct connection
*connection
, char const *packet
,
1077 struct linux_os
*linux_os
= (struct linux_os
*)
1078 target
->rtos
->rtos_specific_params
;
1079 struct threads
*temp
= linux_os
->thread_list
;
1082 if (temp
->threadid
== linux_os
->preupdtate_threadid_count
+ 1) {
1083 /*LOG_INFO("FOUND");*/
1091 /*LOG_INFO("INTO GDB THREAD UPDATE FOUNDING START TASK");*/
1092 char *out_strr
= calloc(MAX_THREADS
* 17 + 10, 1);
1093 char *tmp_strr
= out_strr
;
1094 tmp_strr
+= sprintf(tmp_strr
, "m");
1095 /*LOG_INFO("CHAR MALLOC & M DONE");*/
1096 tmp_strr
+= sprintf(tmp_strr
, "%016" PRIx64
, temp
->threadid
);
1101 /*LOG_INFO("INTO GDB THREAD UPDATE WHILE");*/
1102 tmp_strr
+= sprintf(tmp_strr
, ",");
1104 sprintf(tmp_strr
, "%016" PRIx64
, temp
->threadid
);
1109 gdb_put_packet(connection
, out_strr
, strlen(out_strr
));
1110 linux_os
->preupdtate_threadid_count
=
1111 linux_os
->threadid_count
- 1;
1114 gdb_put_packet(connection
, "l", 1);
1119 static int linux_thread_extra_info(struct target
*target
,
1120 struct connection
*connection
, char const *packet
,
1123 int64_t threadid
= 0;
1124 struct linux_os
*linux_os
= (struct linux_os
*)
1125 target
->rtos
->rtos_specific_params
;
1126 sscanf(packet
, "qThreadExtraInfo,%" SCNx64
, &threadid
);
1127 /*LOG_INFO("lookup extra info for thread %" SCNx64, threadid);*/
1128 struct threads
*temp
= linux_os
->thread_list
;
1131 if (temp
->threadid
== threadid
) {
1132 char *pid
= " PID: ";
1133 char *pid_current
= "*PID: ";
1134 char *name
= "Name: ";
1135 int str_size
= strlen(pid
) + strlen(name
);
1136 char *tmp_str
= calloc(1, str_size
+ 50);
1137 char *tmp_str_ptr
= tmp_str
;
1139 /* discriminate current task */
1140 if (temp
->status
== 3)
1141 tmp_str_ptr
+= sprintf(tmp_str_ptr
, "%s",
1144 tmp_str_ptr
+= sprintf(tmp_str_ptr
, "%s", pid
);
1146 tmp_str_ptr
+= sprintf(tmp_str_ptr
, "%d, ", (int)temp
->pid
);
1147 sprintf(tmp_str_ptr
, "%s", name
);
1148 sprintf(tmp_str_ptr
, "%s", temp
->name
);
1149 char *hex_str
= calloc(1, strlen(tmp_str
) * 2 + 1);
1150 size_t pkt_len
= hexify(hex_str
, (const uint8_t *)tmp_str
,
1151 strlen(tmp_str
), strlen(tmp_str
) * 2 + 1);
1152 gdb_put_packet(connection
, hex_str
, pkt_len
);
1161 LOG_INFO("thread not found");
1165 static int linux_gdb_t_packet(struct connection
*connection
,
1166 struct target
*target
, char const *packet
, int packet_size
)
1169 struct linux_os
*linux_os
= (struct linux_os
*)
1170 target
->rtos
->rtos_specific_params
;
1171 int retval
= ERROR_OK
;
1172 sscanf(packet
, "T%" SCNx64
, &threadid
);
1174 if (linux_os
->threads_needs_update
== 0) {
1175 struct threads
*temp
= linux_os
->thread_list
;
1176 struct threads
*prev
= NULL
;
1179 if (temp
->threadid
== threadid
) {
1180 if (temp
->status
!= 0) {
1181 gdb_put_packet(connection
, "OK", 2);
1184 /* delete item in the list */
1185 linux_os
->thread_list
=
1186 liste_del_task(linux_os
->thread_list
,
1188 linux_os
->thread_count
--;
1189 gdb_put_packet(connection
, "E01", 3);
1199 LOG_INFO("gdb requested status on non existing thread");
1200 gdb_put_packet(connection
, "E01", 3);
1204 retval
= linux_task_update(target
, 1);
1205 struct threads
*temp
= linux_os
->thread_list
;
1208 if (temp
->threadid
== threadid
) {
1209 if (temp
->status
== 1) {
1210 gdb_put_packet(connection
, "OK", 2);
1213 gdb_put_packet(connection
, "E01", 3);
1225 static int linux_gdb_h_packet(struct connection
*connection
,
1226 struct target
*target
, char const *packet
, int packet_size
)
1228 struct linux_os
*linux_os
= (struct linux_os
*)
1229 target
->rtos
->rtos_specific_params
;
1230 struct current_thread
*ct
= linux_os
->current_threads
;
1232 /* select to display the current thread of the selected target */
1233 while ((ct
) && (ct
->core_id
!= target
->coreid
))
1236 int64_t current_gdb_thread_rq
;
1238 if (linux_os
->threads_lookup
== 1) {
1239 if ((ct
) && (ct
->threadid
== -1)) {
1240 ct
= linux_os
->current_threads
;
1242 while ((ct
) && (ct
->threadid
== -1))
1247 /* no current thread can be identified
1248 * any way with smp */
1249 LOG_INFO("no current thread identified");
1250 /* attempt to display the name of the 2 threads identified with
1253 ct
= linux_os
->current_threads
;
1255 while ((ct
) && (ct
->threadid
== -1)) {
1256 t
.base_addr
= ct
->TS
;
1257 get_name(target
, &t
);
1258 LOG_INFO("name of unidentified thread %s",
1263 gdb_put_packet(connection
, "OK", 2);
1267 if (packet
[1] == 'g') {
1268 sscanf(packet
, "Hg%16" SCNx64
, ¤t_gdb_thread_rq
);
1270 if (current_gdb_thread_rq
== 0) {
1271 target
->rtos
->current_threadid
= ct
->threadid
;
1272 gdb_put_packet(connection
, "OK", 2);
1274 target
->rtos
->current_threadid
=
1275 current_gdb_thread_rq
;
1276 gdb_put_packet(connection
, "OK", 2);
1278 } else if (packet
[1] == 'c') {
1279 sscanf(packet
, "Hc%16" SCNx64
, ¤t_gdb_thread_rq
);
1281 if ((current_gdb_thread_rq
== 0) ||
1282 (current_gdb_thread_rq
== ct
->threadid
)) {
1283 target
->rtos
->current_threadid
= ct
->threadid
;
1284 gdb_put_packet(connection
, "OK", 2);
1286 gdb_put_packet(connection
, "E01", 3);
1289 gdb_put_packet(connection
, "OK", 2);
1294 static int linux_thread_packet(struct connection
*connection
, char const *packet
,
1297 int retval
= ERROR_OK
;
1298 struct current_thread
*ct
;
1299 struct target
*target
= get_target_from_connection(connection
);
1300 struct linux_os
*linux_os
= (struct linux_os
*)
1301 target
->rtos
->rtos_specific_params
;
1303 switch (packet
[0]) {
1304 case 'T': /* Is thread alive?*/
1306 linux_gdb_t_packet(connection
, target
, packet
, packet_size
);
1308 case 'H': /* Set current thread */
1309 /* ( 'c' for step and continue, 'g' for all other operations )*/
1310 /*LOG_INFO(" H packet received '%s'", packet);*/
1311 linux_gdb_h_packet(connection
, target
, packet
, packet_size
);
1315 if (strncmp(packet
, "qSymbol", 7) == 0) {
1316 if (rtos_qsymbol(connection
, packet
, packet_size
) == 1) {
1317 linux_compute_virt2phys(target
,
1318 target
->rtos
->symbols
[INIT_TASK
].address
);
1322 } else if (strncmp(packet
, "qfThreadInfo", 12) == 0) {
1323 if (!linux_os
->thread_list
) {
1324 retval
= linux_gdb_thread_packet(target
,
1330 retval
= linux_gdb_thread_update(target
,
1336 } else if (strncmp(packet
, "qsThreadInfo", 12) == 0) {
1337 gdb_put_packet(connection
, "l", 1);
1339 } else if (strncmp(packet
, "qThreadExtraInfo,", 17) == 0) {
1340 linux_thread_extra_info(target
, connection
, packet
,
1344 retval
= GDB_THREAD_PACKET_NOT_CONSUMED
;
1349 /* previously response was : thread not found
1350 * gdb_put_packet(connection, "E01", 3); */
1351 retval
= GDB_THREAD_PACKET_NOT_CONSUMED
;
1355 if (linux_os
->threads_lookup
== 1) {
1356 ct
= linux_os
->current_threads
;
1358 while ((ct
) && (ct
->core_id
) != target
->coreid
)
1361 if ((ct
) && (ct
->threadid
== -1)) {
1362 ct
= linux_os
->current_threads
;
1364 while ((ct
) && (ct
->threadid
== -1))
1368 if ((ct
) && (ct
->threadid
!=
1369 target
->rtos
->current_threadid
)
1370 && (target
->rtos
->current_threadid
!= -1))
1371 LOG_WARNING("WARNING! current GDB thread do not match "
1372 "current thread running. "
1373 "Switch thread in GDB to threadid %d",
1376 LOG_INFO("threads_needs_update = 1");
1377 linux_os
->threads_needs_update
= 1;
1381 /* if a packet handler returned an error, exit input loop */
1382 if (retval
!= ERROR_OK
)
1389 static int linux_os_smp_init(struct target
*target
)
1391 struct target_list
*head
;
1392 /* keep only target->rtos */
1393 struct rtos
*rtos
= target
->rtos
;
1394 struct linux_os
*os_linux
=
1395 (struct linux_os
*)rtos
->rtos_specific_params
;
1396 struct current_thread
*ct
;
1397 head
= target
->head
;
1400 if (head
->target
->rtos
!= rtos
) {
1401 struct linux_os
*smp_os_linux
=
1402 (struct linux_os
*)head
->target
->rtos
->rtos_specific_params
;
1403 /* remap smp target on rtos */
1404 free(head
->target
->rtos
);
1405 head
->target
->rtos
= rtos
;
1406 /* reuse allocated ct */
1407 ct
= smp_os_linux
->current_threads
;
1409 ct
->TS
= 0xdeadbeef;
1410 ct
->core_id
= head
->target
->coreid
;
1411 os_linux
->current_threads
=
1412 add_current_thread(os_linux
->current_threads
, ct
);
1413 os_linux
->nr_cpus
++;
1423 static int linux_os_create(struct target
*target
)
1425 struct linux_os
*os_linux
= calloc(1, sizeof(struct linux_os
));
1426 struct current_thread
*ct
= calloc(1, sizeof(struct current_thread
));
1427 LOG_INFO("linux os creation\n");
1428 os_linux
->init_task_addr
= 0xdeadbeef;
1429 os_linux
->name
= "linux";
1430 os_linux
->thread_list
= NULL
;
1431 os_linux
->thread_count
= 0;
1432 target
->rtos
->current_threadid
= -1;
1433 os_linux
->nr_cpus
= 1;
1434 os_linux
->threads_lookup
= 0;
1435 os_linux
->threads_needs_update
= 0;
1436 os_linux
->threadid_count
= 1;
1437 os_linux
->current_threads
= NULL
;
1438 target
->rtos
->rtos_specific_params
= os_linux
;
1439 ct
->core_id
= target
->coreid
;
1441 ct
->TS
= 0xdeadbeef;
1442 os_linux
->current_threads
=
1443 add_current_thread(os_linux
->current_threads
, ct
);
1444 /* overload rtos thread default handler */
1445 target
->rtos
->gdb_thread_packet
= linux_thread_packet
;
1446 /* initialize a default virt 2 phys translation */
1447 os_linux
->phys_mask
= ~0xc0000000;
1448 os_linux
->phys_base
= 0x0;
1452 static char *linux_ps_command(struct target
*target
)
1454 struct linux_os
*linux_os
= (struct linux_os
*)
1455 target
->rtos
->rtos_specific_params
;
1456 int retval
= ERROR_OK
;
1459 if (linux_os
->threads_lookup
== 0)
1460 retval
= linux_get_tasks(target
, 1);
1462 if (linux_os
->threads_needs_update
!= 0)
1463 retval
= linux_task_update(target
, 0);
1466 if (retval
== ERROR_OK
) {
1467 struct threads
*temp
= linux_os
->thread_list
;
1469 LOG_INFO("allocation for %d threads line",
1470 linux_os
->thread_count
);
1471 display
= calloc((linux_os
->thread_count
+ 2) * 80, 1);
1477 tmp
+= sprintf(tmp
, "PID\t\tCPU\t\tASID\t\tNAME\n");
1478 tmp
+= sprintf(tmp
, "---\t\t---\t\t----\t\t----\n");
1485 "%" PRIu32
"\t\t%" PRIu32
"\t\t%" PRIx32
"\t\t%s\n",
1486 temp
->pid
, temp
->oncpu
,
1487 temp
->asid
, temp
->name
);
1491 "%" PRIu32
"\t\t%" PRIu32
"\t\t%" PRIx32
"\t\t%s\n",
1492 temp
->pid
, temp
->oncpu
,
1493 temp
->asid
, temp
->name
);
1503 display
= calloc(40, 1);
1504 sprintf(display
, "linux_ps_command failed\n");