1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ZIRCON_SYSCALLS_OBJECT_H_
6 #define ZIRCON_SYSCALLS_OBJECT_H_
7 
8 #include <zircon/types.h>
9 
10 __BEGIN_CDECLS
11 
12 // ask clang format not to mess up the indentation:
13 // clang-format off
14 
15 // Valid topics for zx_object_get_info.
16 typedef uint32_t zx_object_info_topic_t;
17 #define ZX_INFO_NONE                    ((zx_object_info_topic_t)  0u)
18 #define ZX_INFO_HANDLE_VALID            ((zx_object_info_topic_t)  1u)
19 #define ZX_INFO_HANDLE_BASIC            ((zx_object_info_topic_t)  2u) // zx_info_handle_basic_t[1]
20 #define ZX_INFO_PROCESS                 ((zx_object_info_topic_t)  3u) // zx_info_process_t[1]
21 #define ZX_INFO_PROCESS_THREADS         ((zx_object_info_topic_t)  4u) // zx_koid_t[n]
22 #define ZX_INFO_VMAR                    ((zx_object_info_topic_t)  7u) // zx_info_vmar_t[1]
23 #define ZX_INFO_JOB_CHILDREN            ((zx_object_info_topic_t)  8u) // zx_koid_t[n]
24 #define ZX_INFO_JOB_PROCESSES           ((zx_object_info_topic_t)  9u) // zx_koid_t[n]
25 #define ZX_INFO_THREAD                  ((zx_object_info_topic_t) 10u) // zx_info_thread_t[1]
26 #define ZX_INFO_THREAD_EXCEPTION_REPORT ((zx_object_info_topic_t) 11u) // zx_exception_report_t[1]
27 #define ZX_INFO_TASK_STATS              ((zx_object_info_topic_t) 12u) // zx_info_task_stats_t[1]
28 #define ZX_INFO_PROCESS_MAPS            ((zx_object_info_topic_t) 13u) // zx_info_maps_t[n]
29 #define ZX_INFO_PROCESS_VMOS            ((zx_object_info_topic_t) 14u) // zx_info_vmo_t[n]
30 #define ZX_INFO_THREAD_STATS            ((zx_object_info_topic_t) 15u) // zx_info_thread_stats_t[1]
31 #define ZX_INFO_CPU_STATS               ((zx_object_info_topic_t) 16u) // zx_info_cpu_stats_t[n]
32 #define ZX_INFO_KMEM_STATS              ((zx_object_info_topic_t) 17u) // zx_info_kmem_stats_t[1]
33 #define ZX_INFO_RESOURCE                ((zx_object_info_topic_t) 18u) // zx_info_resource_t[1]
34 #define ZX_INFO_HANDLE_COUNT            ((zx_object_info_topic_t) 19u) // zx_info_handle_count_t[1]
35 #define ZX_INFO_BTI                     ((zx_object_info_topic_t) 20u) // zx_info_bti_t[1]
36 #define ZX_INFO_PROCESS_HANDLE_STATS    ((zx_object_info_topic_t) 21u) // zx_info_process_handle_stats_t[1]
37 #define ZX_INFO_SOCKET                  ((zx_object_info_topic_t) 22u) // zx_info_socket_t[1]
38 #define ZX_INFO_VMO                     ((zx_object_info_topic_t) 23u) // zx_info_vmo_t[1]
39 
40 typedef uint32_t zx_obj_props_t;
41 #define ZX_OBJ_PROP_NONE                ((zx_obj_props_t)0u)
42 #define ZX_OBJ_PROP_WAITABLE            ((zx_obj_props_t)1u)
43 
44 typedef struct zx_info_handle_basic {
45     // The unique id assigned by kernel to the object referenced by the
46     // handle.
47     zx_koid_t koid;
48 
49     // The immutable rights assigned to the handle. Two handles that
50     // have the same koid and the same rights are equivalent and
51     // interchangeable.
52     zx_rights_t rights;
53 
54     // The object type: channel, event, socket, etc.
55     zx_obj_type_t type;
56 
57     // If the object referenced by the handle is related to another (such
58     // as the other end of a channel, or the parent of a job) then
59     // |related_koid| is the koid of that object, otherwise it is zero.
60     // This relationship is immutable: an object's |related_koid| does
61     // not change even if the related object no longer exists.
62     zx_koid_t related_koid;
63 
64     // Set to ZX_OBJ_PROP_WAITABLE if the object referenced by the
65     // handle can be waited on; zero otherwise.
66     zx_obj_props_t props;
67 } zx_info_handle_basic_t;
68 
69 typedef struct zx_info_handle_count {
70     // The number of outstanding handles to a kernel object.
71     uint32_t handle_count;
72 } zx_info_handle_count_t;
73 
74 typedef struct zx_info_process_handle_stats {
75     // The number of outstanding handles to kernel objects of each type.
76     uint32_t handle_count[64];
77 } zx_info_process_handle_stats_t;
78 
79 typedef struct zx_info_process {
80     // The process's return code; only valid if |exited| is true.
81     // Guaranteed to be non-zero if the process was killed by |zx_task_kill|.
82     int64_t return_code;
83 
84     // True if the process has ever left the initial creation state,
85     // even if it has exited as well.
86     bool started;
87 
88     // If true, the process has exited and |return_code| is valid.
89     bool exited;
90 
91     // True if a debugger is attached to the process.
92     bool debugger_attached;
93 } zx_info_process_t;
94 
95 typedef uint32_t zx_thread_state_t;
96 
97 typedef struct zx_info_thread {
98     // One of ZX_THREAD_STATE_* values.
99     zx_thread_state_t state;
100 
101     // If |state| is ZX_THREAD_STATE_BLOCKED_EXCEPTION, the thread has gotten
102     // an exception and is waiting for the exception to be handled by the
103     // specified port.
104     // The value is one of ZX_EXCEPTION_PORT_TYPE_*.
105     uint32_t wait_exception_port_type;
106 } zx_info_thread_t;
107 
108 typedef struct zx_info_thread_stats {
109     // Total accumulated running time of the thread.
110     zx_duration_t total_runtime;
111 } zx_info_thread_stats_t;
112 
113 // Statistics about resources (e.g., memory) used by a task. Can be relatively
114 // expensive to gather.
115 typedef struct zx_info_task_stats {
116     // The total size of mapped memory ranges in the task.
117     // Not all will be backed by physical memory.
118     size_t mem_mapped_bytes;
119 
120     // For the fields below, a byte is considered committed if it's backed by
121     // physical memory. Some of the memory may be double-mapped, and thus
122     // double-counted.
123 
124     // Committed memory that is only mapped into this task.
125     size_t mem_private_bytes;
126 
127     // Committed memory that is mapped into this and at least one other task.
128     size_t mem_shared_bytes;
129 
130     // A number that estimates the fraction of mem_shared_bytes that this
131     // task is responsible for keeping alive.
132     //
133     // An estimate of:
134     //   For each shared, committed byte:
135     //   mem_scaled_shared_bytes += 1 / (number of tasks mapping this byte)
136     //
137     // This number is strictly smaller than mem_shared_bytes.
138     size_t mem_scaled_shared_bytes;
139 } zx_info_task_stats_t;
140 
141 typedef struct zx_info_vmar {
142     // Base address of the region.
143     uintptr_t base;
144 
145     // Length of the region, in bytes.
146     size_t len;
147 } zx_info_vmar_t;
148 
149 typedef struct zx_info_bti {
150     // zx_bti_pin will always be able to return addresses that are contiguous for at
151     // least this many bytes.  E.g. if this returns 1MB, then a call to
152     // zx_bti_pin() with a size of 2MB will return at most two physically-contiguous runs.
153     // If the size were 2.5MB, it will return at most three physically-contiguous runs.
154     uint64_t minimum_contiguity;
155 
156     // The number of bytes in the device's address space (UINT64_MAX if 2^64).
157     uint64_t aspace_size;
158 } zx_info_bti_t;
159 
160 typedef struct zx_info_socket {
161     // The options passed to zx_socket_create().
162     uint32_t options;
163 
164     // The maximum size of the receive buffer of a socket, in bytes.
165     //
166     // The receive buffer may become full at a capacity less than the maximum
167     // due to overhead.
168     size_t rx_buf_max;
169 
170     // The size of the receive buffer of a socket, in bytes.
171     size_t rx_buf_size;
172 
173     // The amount of data, in bytes, that is available for reading in a single
174     // zx_socket_read call.
175     //
176     // For stream sockets, this value will match |rx_buf_size|. For datagram
177     // sockets, this value will be the size of the next datagram in the receive
178     // buffer.
179     size_t rx_buf_available;
180 
181     // The maximum size of the transmit buffer of a socket, in bytes.
182     //
183     // The transmit buffer may become full at a capacity less than the maximum
184     // due to overhead.
185     //
186     // Will be zero if the peer endpoint is closed.
187     size_t tx_buf_max;
188 
189     // The size of the transmit buffer of a socket, in bytes.
190     //
191     // Will be zero if the peer endpoint is closed.
192     size_t tx_buf_size;
193 } zx_info_socket_t;
194 
195 // Types and values used by ZX_INFO_PROCESS_MAPS.
196 
197 // Describes a VM mapping.
198 typedef struct zx_info_maps_mapping {
199     // MMU flags for the mapping.
200     // Bitwise OR of ZX_VM_PERM_{READ,WRITE,EXECUTE} values.
201     zx_vm_option_t mmu_flags;
202     // koid of the mapped VMO.
203     zx_koid_t vmo_koid;
204     // Offset into the above VMO.
205     uint64_t vmo_offset;
206     // The number of PAGE_SIZE pages in the mapped region of the VMO
207     // that are backed by physical memory.
208     size_t committed_pages;
209 } zx_info_maps_mapping_t;
210 
211 // Types of entries represented by zx_info_maps_t.
212 // Can't use zx_obj_type_t because not all of these are
213 // user-visible kernel object types.
214 typedef uint32_t zx_info_maps_type_t;
215 #define ZX_INFO_MAPS_TYPE_NONE    ((zx_info_maps_type_t) 0u)
216 #define ZX_INFO_MAPS_TYPE_ASPACE  ((zx_info_maps_type_t) 1u)
217 #define ZX_INFO_MAPS_TYPE_VMAR    ((zx_info_maps_type_t) 2u)
218 #define ZX_INFO_MAPS_TYPE_MAPPING ((zx_info_maps_type_t) 3u)
219 
220 // Describes a node in the aspace/vmar/mapping hierarchy for a user process.
221 typedef struct zx_info_maps {
222     // Name if available; empty string otherwise.
223     char name[ZX_MAX_NAME_LEN];
224     // Base address.
225     zx_vaddr_t base;
226     // Size in bytes.
227     size_t size;
228 
229     // The depth of this node in the tree.
230     // Can be used for indentation, or to rebuild the tree from an array
231     // of zx_info_maps_t entries, which will be in depth-first pre-order.
232     size_t depth;
233     // The type of this entry; indicates which union entry is valid.
234     zx_info_maps_type_t type;
235     union {
236         zx_info_maps_mapping_t mapping;
237         // No additional fields for other types.
238     } u;
239 } zx_info_maps_t;
240 
241 
242 // Values and types used by ZX_INFO_PROCESS_VMOS.
243 
244 // The VMO is backed by RAM, consuming memory.
245 // Mutually exclusive with ZX_INFO_VMO_TYPE_PHYSICAL.
246 // See ZX_INFO_VMO_TYPE(flags)
247 #define ZX_INFO_VMO_TYPE_PAGED              (1u<<0)
248 
249 // The VMO points to a physical address range, and does not consume memory.
250 // Typically used to access memory-mapped hardware.
251 // Mutually exclusive with ZX_INFO_VMO_TYPE_PAGED.
252 // See ZX_INFO_VMO_TYPE(flags)
253 #define ZX_INFO_VMO_TYPE_PHYSICAL           (0u<<0)
254 
255 // Returns a VMO's type based on its flags, allowing for checks like
256 // if (ZX_INFO_VMO_TYPE(f) == ZX_INFO_VMO_TYPE_PAGED)
257 #define ZX_INFO_VMO_TYPE(flags)             ((flags) & (1u<<0))
258 
259 // The VMO is a clone, and is a copy-on-write clone.
260 #define ZX_INFO_VMO_IS_COW_CLONE            (1u<<2)
261 
262 // When reading a list of VMOs pointed to by a process, indicates that the
263 // process has a handle to the VMO, which isn't necessarily mapped.
264 #define ZX_INFO_VMO_VIA_HANDLE              (1u<<3)
265 
266 // When reading a list of VMOs pointed to by a process, indicates that the
267 // process maps the VMO into a VMAR, but doesn't necessarily have a handle to
268 // the VMO.
269 #define ZX_INFO_VMO_VIA_MAPPING             (1u<<4)
270 
271 // Describes a VMO. For mapping information, see |zx_info_maps_t|.
272 typedef struct zx_info_vmo {
273     // The koid of this VMO.
274     zx_koid_t koid;
275 
276     // The name of this VMO.
277     char name[ZX_MAX_NAME_LEN];
278 
279     // The size of this VMO; i.e., the amount of virtual address space it
280     // would consume if mapped.
281     uint64_t size_bytes;
282 
283     // If this VMO is a clone, the koid of its parent. Otherwise, zero.
284     // See |flags| for the type of clone.
285     zx_koid_t parent_koid;
286 
287     // The number of clones of this VMO, if any.
288     size_t num_children;
289 
290     // The number of times this VMO is currently mapped into VMARs.
291     // Note that the same process will often map the same VMO twice,
292     // and both mappings will be counted here. (I.e., this is not a count
293     // of the number of processes that map this VMO; see share_count.)
294     size_t num_mappings;
295 
296     // An estimate of the number of unique address spaces that
297     // this VMO is mapped into. Every process has its own address space,
298     // and so does the kernel.
299     size_t share_count;
300 
301     // Bitwise OR of ZX_INFO_VMO_* values.
302     uint32_t flags;
303 
304     // If |ZX_INFO_VMO_TYPE(flags) == ZX_INFO_VMO_TYPE_PAGED|, the amount of
305     // memory currently allocated to this VMO; i.e., the amount of physical
306     // memory it consumes. Undefined otherwise.
307     uint64_t committed_bytes;
308 
309     // If |flags & ZX_INFO_VMO_VIA_HANDLE|, the handle rights.
310     // Undefined otherwise.
311     zx_rights_t handle_rights;
312 
313     // VMO creation options. This is a bitmask of
314     // kResizable    = (1u << 0);
315     // kContiguous   = (1u << 1);
316     uint32_t create_options;
317 
318     // VMO mapping cache policy. One of ZX_CACHE_POLICY_*
319     uint32_t cache_policy;
320 } zx_info_vmo_t;
321 
322 // kernel statistics per cpu
323 // TODO(cpu), expose the deprecated stats via a new syscall.
324 typedef struct zx_info_cpu_stats {
325     uint32_t cpu_number;
326     uint32_t flags;
327 
328     zx_duration_t idle_time;
329 
330     // kernel scheduler counters
331     uint64_t reschedules;
332     uint64_t context_switches;
333     uint64_t irq_preempts;
334     uint64_t preempts;
335     uint64_t yields;
336 
337     // cpu level interrupts and exceptions
338     uint64_t ints;          // hardware interrupts, minus timer interrupts or inter-processor interrupts
339     uint64_t timer_ints;    // timer interrupts
340     uint64_t timers;        // timer callbacks
341     uint64_t page_faults;   // (deprecated, returns 0) page faults
342     uint64_t exceptions;    // (deprecated, returns 0) exceptions such as undefined opcode
343     uint64_t syscalls;
344 
345     // inter-processor interrupts
346     uint64_t reschedule_ipis;
347     uint64_t generic_ipis;
348 } zx_info_cpu_stats_t;
349 
350 // Information about kernel memory usage.
351 // Can be expensive to gather.
352 typedef struct zx_info_kmem_stats {
353     // The total amount of physical memory available to the system.
354     uint64_t total_bytes;
355 
356     // The amount of unallocated memory.
357     uint64_t free_bytes;
358 
359     // The amount of memory reserved by and mapped into the kernel for reasons
360     // not covered by other fields in this struct. Typically for readonly data
361     // like the ram disk and kernel image, and for early-boot dynamic memory.
362     uint64_t wired_bytes;
363 
364     // The amount of memory allocated to the kernel heap.
365     uint64_t total_heap_bytes;
366 
367     // The portion of |total_heap_bytes| that is not in use.
368     uint64_t free_heap_bytes;
369 
370     // The amount of memory committed to VMOs, both kernel and user.
371     // A superset of all userspace memory.
372     // Does not include certain VMOs that fall under |wired_bytes|.
373     //
374     // TODO(dbort): Break this into at least two pieces: userspace VMOs that
375     // have koids, and kernel VMOs that don't. Or maybe look at VMOs
376     // mapped into the kernel aspace vs. everything else.
377     uint64_t vmo_bytes;
378 
379     // The amount of memory used for architecture-specific MMU metadata
380     // like page tables.
381     uint64_t mmu_overhead_bytes;
382 
383     // The amount of memory in use by IPC.
384     uint64_t ipc_bytes;
385 
386     // Non-free memory that isn't accounted for in any other field.
387     uint64_t other_bytes;
388 } zx_info_kmem_stats_t;
389 
390 typedef struct zx_info_resource {
391     // The resource kind; resource object kinds are detailed in the resource.md
392     uint32_t kind;
393     // Resource's creation flags
394     uint32_t flags;
395     // Resource's base value (inclusive)
396     uint64_t base;
397     // Resource's length value
398     size_t size;
399     char name[ZX_MAX_NAME_LEN];
400 } zx_info_resource_t;
401 
402 #define ZX_INFO_CPU_STATS_FLAG_ONLINE       (1u<<0)
403 
404 // Object properties.
405 
406 // Argument is a char[ZX_MAX_NAME_LEN].
407 #define ZX_PROP_NAME                        ((uint32_t) 3u)
408 
409 #if __x86_64__
410 // Argument is a uintptr_t.
411 #define ZX_PROP_REGISTER_GS                 ((uint32_t) 2u)
412 #define ZX_PROP_REGISTER_FS                 ((uint32_t) 4u)
413 #endif
414 
415 // Argument is the value of ld.so's _dl_debug_addr, a uintptr_t. If the
416 // property is set to the magic value of ZX_PROCESS_DEBUG_ADDR_BREAK_ON_SET
417 // on process startup, ld.so will trigger a debug breakpoint immediately after
418 // setting the property to the correct value.
419 #define ZX_PROP_PROCESS_DEBUG_ADDR          ((uint32_t) 5u)
420 #define ZX_PROCESS_DEBUG_ADDR_BREAK_ON_SET  ((uintptr_t) 1u)
421 
422 // Argument is the base address of the vDSO mapping (or zero), a uintptr_t.
423 #define ZX_PROP_PROCESS_VDSO_BASE_ADDRESS   ((uint32_t) 6u)
424 
425 // Argument is a size_t.
426 #define ZX_PROP_SOCKET_RX_THRESHOLD         12u
427 #define ZX_PROP_SOCKET_TX_THRESHOLD         13u
428 
429 // Terminate this job if the system is low on memory.
430 #define ZX_PROP_JOB_KILL_ON_OOM             15u
431 
432 // Basic thread states, in zx_info_thread_t.state.
433 #define ZX_THREAD_STATE_NEW                 ((zx_thread_state_t) 0x0000u)
434 #define ZX_THREAD_STATE_RUNNING             ((zx_thread_state_t) 0x0001u)
435 #define ZX_THREAD_STATE_SUSPENDED           ((zx_thread_state_t) 0x0002u)
436 // ZX_THREAD_STATE_BLOCKED is never returned by itself.
437 // It is always returned with a more precise reason.
438 // See ZX_THREAD_STATE_BLOCKED_* below.
439 #define ZX_THREAD_STATE_BLOCKED             ((zx_thread_state_t) 0x0003u)
440 #define ZX_THREAD_STATE_DYING               ((zx_thread_state_t) 0x0004u)
441 #define ZX_THREAD_STATE_DEAD                ((zx_thread_state_t) 0x0005u)
442 
443 // More precise thread states.
444 #define ZX_THREAD_STATE_BLOCKED_EXCEPTION   ((zx_thread_state_t) 0x0103u)
445 #define ZX_THREAD_STATE_BLOCKED_SLEEPING    ((zx_thread_state_t) 0x0203u)
446 #define ZX_THREAD_STATE_BLOCKED_FUTEX       ((zx_thread_state_t) 0x0303u)
447 #define ZX_THREAD_STATE_BLOCKED_PORT        ((zx_thread_state_t) 0x0403u)
448 #define ZX_THREAD_STATE_BLOCKED_CHANNEL     ((zx_thread_state_t) 0x0503u)
449 #define ZX_THREAD_STATE_BLOCKED_WAIT_ONE    ((zx_thread_state_t) 0x0603u)
450 #define ZX_THREAD_STATE_BLOCKED_WAIT_MANY   ((zx_thread_state_t) 0x0703u)
451 #define ZX_THREAD_STATE_BLOCKED_INTERRUPT   ((zx_thread_state_t) 0x0803u)
452 
453 // Reduce possibly-more-precise state to a basic state.
454 // Useful if, for example, you want to check for BLOCKED on anything.
455 #define ZX_THREAD_STATE_BASIC(n) ((n) & 0xff)
456 
457 __END_CDECLS
458 
459 #endif // ZIRCON_SYSCALLS_OBJECT_H_
460