Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
target_os_linux_generic.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 2015 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef __TARGET_OS_LINUX_GENERIC_H__
20 #define __TARGET_OS_LINUX_GENERIC_H__
21 
22 #ifndef PAGE_SIZE
23 #define PAGE_SIZE 0x1000
24 #endif
25 #define THREAD_SIZE 8192
26 /* x86_64 constant used in current_thread_ptr */
27 #define KERNEL_STACK_OFFSET (5*8)
28 
29 #define THREAD_INFO_GET_CPL(tid) (((tid) & (0x3 << 62)) >> 62)
30 #define THREAD_INFO_GET_TID(tid) ((tid) & 0xffffffff)
31 #define THREAD_INFO_SET_CPL(tid,cpl) (tid) |= (((cpl) & 0x3) << 62)
32 #define THREAD_INFO_SET_TID(tid,pid) (tid) |= (0xffffffff & (pid))
33 
34 #define TIF_32_SYSCALL_TRACE 0 /* syscall trace active */
35 #define TIF_32_NOTIFY_RESUME 1 /* resumption notification requested */
36 #define TIF_32_SIGPENDING 2 /* signal pending */
37 #define TIF_32_NEED_RESCHED 3 /* rescheduling necessary */
38 #define TIF_32_SINGLESTEP 4 /* restore singlestep on return to user mode */
39 #define TIF_32_IRET 5 /* return with iret */
40 #define TIF_32_SYSCALL_EMU 6 /* syscall emulation active */
41 #define TIF_32_SYSCALL_AUDIT 7 /* syscall auditing active */
42 #define TIF_32_SECCOMP 8 /* secure computing */
43 #define TIF_32_RESTORE_SIGMASK 9 /* restore signal mask in do_signal() */
44 #define TIF_32_MEMDIE 16
45 #define TIF_32_DEBUG 17 /* uses debug registers */
46 #define TIF_32_IO_BITMAP 18 /* uses I/O bitmap */
47 
48 #define _TIF_32_SYSCALL_TRACE (1<<TIF_32_SYSCALL_TRACE)
49 #define _TIF_32_NOTIFY_RESUME (1<<TIF_32_NOTIFY_RESUME)
50 #define _TIF_32_SIGPENDING (1<<TIF_32_SIGPENDING)
51 #define _TIF_32_NEED_RESCHED (1<<TIF_32_NEED_RESCHED)
52 #define _TIF_32_SINGLESTEP (1<<TIF_32_SINGLESTEP)
53 #define _TIF_32_IRET (1<<TIF_32_IRET)
54 #define _TIF_32_SYSCALL_EMU (1<<TIF_32_SYSCALL_EMU)
55 #define _TIF_32_SYSCALL_AUDIT (1<<TIF_32_SYSCALL_AUDIT)
56 #define _TIF_32_SECCOMP (1<<TIF_32_SECCOMP)
57 #define _TIF_32_RESTORE_SIGMASK (1<<TIF_32_RESTORE_SIGMASK)
58 #define _TIF_32_DEBUG (1<<TIF_32_DEBUG)
59 #define _TIF_32_IO_BITMAP (1<<TIF_32_IO_BITMAP)
60 
61 
62 #define TIF_64_SYSCALL_TRACE 0 /* syscall trace active */
63 #define TIF_64_NOTIFY_RESUME 1 /* resumption notification requested */
64 #define TIF_64_SIGPENDING 2 /* signal pending */
65 #define TIF_64_NEED_RESCHED 3 /* rescheduling necessary */
66 #define TIF_64_SINGLESTEP 4 /* reenable singlestep on user return*/
67 #define TIF_64_IRET 5 /* force IRET */
68 #define TIF_64_SYSCALL_AUDIT 7 /* syscall auditing active */
69 #define TIF_64_SECCOMP 8 /* secure computing */
70 /* 16 free */
71 #define TIF_64_IA32 17 /* 32bit process */
72 #define TIF_64_FORK 18 /* ret_from_fork */
73 #define TIF_64_ABI_PENDING 19
74 #define TIF_64_MEMDIE 20
75 
76 #define _TIF_64_SYSCALL_TRACE (1<<TIF_64_SYSCALL_TRACE)
77 #define _TIF_64_NOTIFY_RESUME (1<<TIF_64_NOTIFY_RESUME)
78 #define _TIF_64_SIGPENDING (1<<TIF_64_SIGPENDING)
79 #define _TIF_64_SINGLESTEP (1<<TIF_64_SINGLESTEP)
80 #define _TIF_64_NEED_RESCHED (1<<TIF_64_NEED_RESCHED)
81 #define _TIF_64_IRET (1<<TIF_64_IRET)
82 #define _TIF_64_SYSCALL_AUDIT (1<<TIF_64_SYSCALL_AUDIT)
83 #define _TIF_64_SECCOMP (1<<TIF_64_SECCOMP)
84 #define _TIF_64_IA32 (1<<TIF_64_IA32)
85 #define _TIF_64_FORK (1<<TIF_64_FORK)
86 #define _TIF_64_ABI_PENDING (1<<TIF_64_ABI_PENDING)
87 
89  /*
90  * Some kernel task_structs have thread_info; others have void *stack.
91  * Some kernel pt_regs structs have ds/es/fs/gs, or various combinations.
92  * Some kernel thread_structs have debugreg[8];
93  * debugreg0--debugreg7; or a mix of ptrace_bps[4] and debugreg6 and
94  * ptrace_dr7 ... argh.
95  */
106 
107  unsigned int last_thread_count;
109 
110  /*
111  * Some kernel thread_structs have esp/esp0 (older); others have
112  * sp/sp0 (newer). These values are either esp0/sp0/eip, or esp/sp/ip.
113  */
117 
118  /* Some newer kernel thread_info structs have saved_preempt_count. */
120 
121  /*
122  * On x86_64 systems, figuring out the saved IP for a sleeping
123  * thread is difficult, because the kernel's context switch
124  * functions are written so that they swap context by swapping the
125  * old and new %rsp. See the (much) more detailed comments in
126  * target_os_linux_generic.c ...
127  */
131 
132  /*
133  * Newer kernels store uid/gid info in task->[real_]cred->(uid|gid);
134  * older ones just in task->(uid|gid).
135  */
136  const char *task_uid_member_name;
137  const char *task_gid_member_name;
138 
146 
147  /*
148  * On x86_64, current_thread_ptr is determined by looking at this
149  * per_cpu offset. On x86_64, percpu data is reached via %gs :(.
150  */
153 
165  struct bsymbol *modules;
166 
173  GHashTable *moddep;
175 
179 
183 
185 
186  struct probe *int3_probe;
188 
189  /*
190  * OS Process metadata.
191  */
192 
193  /*
194  * One mm_struct per vma list. These vma caches may be pointed
195  */
196  GHashTable *mm_addr_to_mm_cache;
197 
198  /*
199  * tid_t to struct target_os_process *.
200  */
201  GHashTable *processes;
202 
203  /* These are for APF_PROCESS_MEMORY. */
211 };
212 
213 /*
214  * This just helps us scan for updates to a task's
215  * task_struct->mm->mmap (vm_area_struct list).
216  *
217  * There is one of these for each target memrange.
218  */
219 struct os_linux_vma {
220  struct value *vma;
223  struct memrange *range;
224 };
225 
226 struct os_linux_mm {
227  uint8_t valid:1;
228 
229  struct addrspace *space;
230 
231  struct value *mm;
232  /* Cache these to determine if range is heap/stack. */
236 
238  int vma_len;
239 };
240 
243 
244  /*
245  * This state all comes from the Linux PCB. It is always blown away
246  * on target_resume or target_singlestep .
247  */
248  /* The task struct is always valid unless we are in interrupt
249  * context.
250  */
251  /* @task_struct is a "live" value! it may be value_refresh()'d! */
254  /* The thread_info is always at the bottom of the kernel stack. */
257  num_t thread_info_preempt_count; /* Read-only; not flushed */
258  /* The thread struct comes out of the task struct. */
262  /*
263  * NB: pgd (cr3) is a little funny. If the target is PAE, it might
264  * be > 2**32. So, this value has to always be a u64.
265  *
266  * Also note: this value is always a kernel virtual address; so use
267  * __xen_vm_cr3 to read this value as a physical address.
268  */
269  uint64_t pgd;
271 
272  /*
273  * These are information about the task's kernel stack. esp0 is the
274  * ring 0 stack pointer; stack_base is the bottom of the stack.
275  */
278 
279  /*
280  * These are all for kernel threads, specifically. The only time a
281  * kernel thread will have saved context info is when it has been
282  * preempted or interrupted. Otherwise, the kernel thread has been
283  * context-switched out of, and this does not save its current
284  * register set; context switching only saves esp/eip, fs/gs in the
285  * task's thread struct; eflags and ebp were pushed on the stack
286  * before context switch.
287  */
290  uint16_t fs;
291  uint16_t gs;
292  uint32_t eflags;
294 
295  /*
296  * We only use this for loading process's regions.
297  */
299 };
300 
305  REGVAL kernel_esp);
307  struct symbol *datatype,
308  REGVAL kernel_esp);
309 int os_linux_get_task_pid(struct target *target,struct value *task);
310 int os_linux_get_task_tid(struct target *target,struct value *task);
311 struct value *os_linux_get_task(struct target *target,tid_t tid);
312 
314  struct symbol *datatype,
315  REGVAL kernel_esp);
316 
317 char *os_linux_file_get_path(struct target *target,struct value *task,
318  struct value *file,char *buf,int buflen);
319 
320 #define PREEMPT_MASK 0x000000ff
321 #define SOFTIRQ_MASK 0x0000ff00
322 #define HARDIRQ_MASK 0x0fff0000
323 #define PREEMPT_ACTIVE 0x10000000
324 #define PREEMPT_NEED_RESCHED 0x80000000
325 #define PREEMPT_BITSHIFT 0
326 #define SOFTIRQ_BITSHIFT 8
327 #define HARDIRQ_BITSHIFT 16
328 
329 /*
330  * These macros are different than the kernel's!
331  */
332 #define PREEMPT_COUNT(p) (((p) & PREEMPT_MASK) >> PREEMPT_BITSHIFT)
333 #define SOFTIRQ_COUNT(p) (((p) & SOFTIRQ_MASK) >> SOFTIRQ_BITSHIFT)
334 #define HARDIRQ_COUNT(p) (((p) & HARDIRQ_MASK) >> HARDIRQ_BITSHIFT)
335 
337 
338 /*
339  * If the iterator returns 1, we break out of the loop.
340  * If the iterator returns -1, we break out of the loop, AND do NOT free
341  * @value (so the caller can save it).
342  */
343 typedef int (*os_linux_list_iterator_t)(struct target *t,struct value *value,
344  void *data);
346  char *list_head_member_name,int nofree,
347  os_linux_list_iterator_t iterator,void *data);
348 
349 int os_linux_list_for_each_entry(struct target *t,struct bsymbol *btype,
350  struct bsymbol *list_head,
351  char *list_head_member_name,int nofree,
352  os_linux_list_iterator_t iterator,void *data);
353 
354 #endif /* __TARGET_OS_LINUX_GENERIC_H__ */
unsigned int thread_struct_has_debugreg
struct bsymbol * module_type
struct symbol * task_struct_type
struct value * os_linux_get_task(struct target *target, tid_t tid)
int os_linux_list_for_each_entry(struct target *t, struct bsymbol *btype, struct bsymbol *list_head, char *list_head_member_name, int nofree, os_linux_list_iterator_t iterator, void *data)
struct symbol * mm_struct_type
unsigned int thread_struct_has_fs
struct symbol * os_linux_get_task_struct_type_ptr(struct target *target)
struct symbol * os_linux_get_task_struct_type(struct target *target)
int32_t tid_t
Definition: common.h:36
int os_linux_get_task_pid(struct target *target, struct value *task)
struct probe * active_memory_probe_mremap
struct probe * int3_probe
unsigned int hypervisor_ignores_userspace_exceptions
GHashTable * task_struct_addr_to_thread
struct symbol * pt_regs_type
const char * task_uid_member_name
struct bsymbol * thread_entry_f_symbol
const char * thread_sp0_member_name
struct value * os_linux_load_current_task_as_type(struct target *target, struct symbol *datatype, REGVAL kernel_esp)
int64_t num_t
Definition: common.h:87
struct probe * active_thread_entry_probe
struct probe * active_memory_probe_madvise
int32_t OFFSET
Definition: common.h:65
Definition: list.h:51
struct probe * active_memory_probe_mmap_pgoff
struct bsymbol * module_free_symbol
struct probe * active_thread_exit_probe
struct symbol * task_struct_type_ptr
struct value * os_linux_load_current_thread_as_type(struct target *target, struct symbol *datatype, REGVAL kernel_esp)
unsigned int thread_struct_has_ds_es
struct bsymbol * thread_exit_f_symbol
GHashTable * mm_addr_to_mm_cache
unsigned int thread_struct_has_debugreg0
struct memrange * range
num_t os_linux_get_preempt_count(struct target *target)
struct symbol * thread_struct_type
struct bsymbol * thread_entry_v_symbol
char * buf
Definition: target_api.h:3298
int(* os_linux_list_iterator_t)(struct target *t, struct value *value, void *data)
unsigned int thread_struct_has_perf_debugreg
struct probe * active_memory_probe
struct probe * active_memory_probe_mmap
const char * task_gid_member_name
struct probe * active_memory_probe_uselib
struct os_linux_vma * vma_cache
Definition: probe.h:308
struct bsymbol * modules
struct probe * debug_probe
struct value * os_linux_load_current_task(struct target *target, REGVAL kernel_esp)
uint32_t REGVAL
Definition: common.h:66
struct addrspace * space
struct bsymbol * thread_exit_v_symbol
unsigned int task_struct_has_thread_info
uint32_t ADDR
Definition: common.h:64
unsigned int last_thread_count
char * os_linux_file_get_path(struct target *target, struct value *task, struct value *file, char *buf, int buflen)
const char * thread_sp_member_name
struct probe * active_memory_probe_munmap
unsigned int pt_regs_has_fs_gs
unsigned int task_struct_has_stack
const char * thread_info_preempt_count_name
struct bsymbol * module_free_mod_symbol
unsigned int pt_regs_has_ds_es
struct symbol * thread_info_type
struct os_linux_vma * next
uint64_t unum_t
Definition: common.h:88
struct bsymbol * init_task
struct probe * active_memory_probe_mprotect
struct symbol * os_linux_get_thread_info_type(struct target *target)
struct target * t
Definition: dumptarget.c:48
const char * thread_ip_member_name
int os_linux_get_task_tid(struct target *target, struct value *task)
int os_linux_list_for_each_struct(struct target *t, struct bsymbol *bsymbol, char *list_head_member_name, int nofree, os_linux_list_iterator_t iterator, void *data)