Commit | Line | Data |
---|---|---|
ebabbf58 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
ebabbf58 MD |
3 | * |
4 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
ebabbf58 MD |
5 | */ |
6 | ||
c0c0989a MJ |
7 | #ifndef _LIBCOUNTER_SHM_H |
8 | #define _LIBCOUNTER_SHM_H | |
9 | ||
ebabbf58 MD |
10 | #include <stddef.h> |
11 | #include <stdint.h> | |
12 | #include <unistd.h> | |
97572c04 | 13 | #include <stdbool.h> |
9d315d6d | 14 | #include "common/logging.h" |
ebabbf58 MD |
15 | #include <urcu/compiler.h> |
16 | #include "shm_types.h" | |
17 | ||
18 | /* lttng_counter_handle_create - for UST. */ | |
19 | extern | |
20 | struct lttng_counter_shm_handle *lttng_counter_handle_create(void *data, | |
21 | uint64_t memory_map_size, int wakeup_fd); | |
22 | /* lttng_counter_handle_add_cpu - for UST. */ | |
23 | extern | |
24 | int lttng_counter_handle_add_cpu(struct lttng_counter_shm_handle *handle, | |
25 | int shm_fd, uint32_t cpu_nr, | |
26 | uint64_t memory_map_size); | |
ddabe860 | 27 | |
1d18d519 MJ |
28 | unsigned int lttng_counter_handle_get_nr_cpus(struct lttng_counter_shm_handle *handle) |
29 | __attribute__((visibility("hidden"))); | |
ebabbf58 MD |
30 | |
31 | /* | |
32 | * Pointer dereferencing. We don't trust the shm_ref, so we validate | |
33 | * both the index and offset with known boundaries. | |
34 | * | |
35 | * "shmp" and "shmp_index" guarantee that it's safe to use the pointer | |
36 | * target type, even in the occurrence of shm_ref modification by an | |
37 | * untrusted process having write access to the shm_ref. We return a | |
38 | * NULL pointer if the ranges are invalid. | |
39 | */ | |
40 | static inline | |
41 | char *_lttng_counter_shmp_offset(struct lttng_counter_shm_object_table *table, | |
42 | struct lttng_counter_shm_ref *ref, | |
43 | size_t idx, size_t elem_size) | |
44 | { | |
45 | struct lttng_counter_shm_object *obj; | |
46 | size_t objindex, ref_offset; | |
47 | ||
48 | objindex = (size_t) ref->index; | |
49 | if (caa_unlikely(objindex >= table->allocated_len)) | |
50 | return NULL; | |
51 | obj = &table->objects[objindex]; | |
52 | ref_offset = (size_t) ref->offset; | |
53 | ref_offset += idx * elem_size; | |
54 | /* Check if part of the element returned would exceed the limits. */ | |
55 | if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size)) | |
56 | return NULL; | |
57 | return &obj->memory_map[ref_offset]; | |
58 | } | |
59 | ||
60 | #define lttng_counter_shmp_index(handle, ref, index) \ | |
61 | ({ \ | |
62 | __typeof__((ref)._type) ____ptr_ret; \ | |
63 | ____ptr_ret = (__typeof__(____ptr_ret)) _lttng_counter_shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \ | |
64 | ____ptr_ret; \ | |
65 | }) | |
66 | ||
67 | #define lttng_counter_shmp(handle, ref) lttng_counter_shmp_index(handle, ref, 0) | |
68 | ||
69 | static inline | |
70 | void _lttng_counter_set_shmp(struct lttng_counter_shm_ref *ref, struct lttng_counter_shm_ref src) | |
71 | { | |
72 | *ref = src; | |
73 | } | |
74 | ||
75 | #define lttng_counter_set_shmp(ref, src) _lttng_counter_set_shmp(&(ref)._ref, src) | |
76 | ||
97572c04 | 77 | struct lttng_counter_shm_object_table *lttng_counter_shm_object_table_create(size_t max_nb_obj, bool populate) |
1d18d519 | 78 | __attribute__((visibility("hidden"))); |
ddabe860 | 79 | |
ebabbf58 MD |
80 | struct lttng_counter_shm_object *lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table *table, |
81 | size_t memory_map_size, | |
82 | enum lttng_counter_shm_object_type type, | |
83 | const int cpu_fd, | |
97572c04 | 84 | int cpu, bool populate) |
1d18d519 | 85 | __attribute__((visibility("hidden"))); |
ddabe860 | 86 | |
ebabbf58 | 87 | struct lttng_counter_shm_object *lttng_counter_shm_object_table_append_shm(struct lttng_counter_shm_object_table *table, |
97572c04 | 88 | int shm_fd, size_t memory_map_size, bool populate) |
1d18d519 | 89 | __attribute__((visibility("hidden"))); |
ddabe860 | 90 | |
ebabbf58 MD |
91 | /* mem ownership is passed to lttng_counter_shm_object_table_append_mem(). */ |
92 | struct lttng_counter_shm_object *lttng_counter_shm_object_table_append_mem(struct lttng_counter_shm_object_table *table, | |
1d18d519 MJ |
93 | void *mem, size_t memory_map_size) |
94 | __attribute__((visibility("hidden"))); | |
ddabe860 | 95 | |
1d18d519 MJ |
96 | void lttng_counter_shm_object_table_destroy(struct lttng_counter_shm_object_table *table, int consumer) |
97 | __attribute__((visibility("hidden"))); | |
ebabbf58 MD |
98 | |
99 | /* | |
100 | * lttng_counter_zalloc_shm - allocate memory within a shm object. | |
101 | * | |
102 | * Shared memory is already zeroed by shmget. | |
103 | * *NOT* multithread-safe (should be protected by mutex). | |
104 | * Returns a -1, -1 tuple on error. | |
105 | */ | |
1d18d519 MJ |
106 | struct lttng_counter_shm_ref lttng_counter_zalloc_shm(struct lttng_counter_shm_object *obj, size_t len) |
107 | __attribute__((visibility("hidden"))); | |
ddabe860 | 108 | |
1d18d519 MJ |
109 | void lttng_counter_align_shm(struct lttng_counter_shm_object *obj, size_t align) |
110 | __attribute__((visibility("hidden"))); | |
ebabbf58 MD |
111 | |
112 | static inline | |
113 | int lttng_counter_shm_get_shm_fd(struct lttng_counter_shm_handle *handle, struct lttng_counter_shm_ref *ref) | |
114 | { | |
115 | struct lttng_counter_shm_object_table *table = handle->table; | |
116 | struct lttng_counter_shm_object *obj; | |
117 | size_t index; | |
118 | ||
119 | index = (size_t) ref->index; | |
120 | if (caa_unlikely(index >= table->allocated_len)) | |
121 | return -EPERM; | |
122 | obj = &table->objects[index]; | |
123 | return obj->shm_fd; | |
124 | } | |
125 | ||
126 | ||
127 | static inline | |
128 | int lttng_counter_shm_get_shm_size(struct lttng_counter_shm_handle *handle, struct lttng_counter_shm_ref *ref, | |
129 | uint64_t *size) | |
130 | { | |
131 | struct lttng_counter_shm_object_table *table = handle->table; | |
132 | struct lttng_counter_shm_object *obj; | |
133 | size_t index; | |
134 | ||
135 | index = (size_t) ref->index; | |
136 | if (caa_unlikely(index >= table->allocated_len)) | |
137 | return -EPERM; | |
138 | obj = &table->objects[index]; | |
139 | *size = obj->memory_map_size; | |
140 | return 0; | |
141 | } | |
142 | ||
143 | #endif /* _LIBCOUNTER_SHM_H */ |