|
@@ -35,30 +35,46 @@ struct
|
|
|
// __uint(pinning, LIBBPF_PIN_BY_NAME);
|
|
// __uint(pinning, LIBBPF_PIN_BY_NAME);
|
|
|
} alloc_map SEC(".maps");
|
|
} alloc_map SEC(".maps");
|
|
|
|
|
|
|
|
|
|
+struct
|
|
|
|
|
+{
|
|
|
|
|
+ __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
|
|
|
|
|
+ __type(key, u64);
|
|
|
|
|
+ __type(value, u64);
|
|
|
|
|
+ __uint(max_entries, MAX_ENTRIES);
|
|
|
|
|
+// __uint(pinning, LIBBPF_PIN_BY_NAME);
|
|
|
|
|
+} proc_alloc_map SEC(".maps");
|
|
|
|
|
+
|
|
|
static __always_inline u64 get_area_start(u64 start_addr,u64 end_addr)
|
|
static __always_inline u64 get_area_start(u64 start_addr,u64 end_addr)
|
|
|
{
|
|
{
|
|
|
- u32 k0 = 0;
|
|
|
|
|
|
|
+ u32 k0 = 0;
|
|
|
struct trace_conf_t *trace_conf = trace_conf_map__lookup(&k0);
|
|
struct trace_conf_t *trace_conf = trace_conf_map__lookup(&k0);
|
|
|
if (!trace_conf) {
|
|
if (!trace_conf) {
|
|
|
- return 0;
|
|
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ s64 partition_size = (end_addr - start_addr) / trace_conf->total_cpus;
|
|
|
|
|
+ u32 current_cpu = bpf_get_smp_processor_id();
|
|
|
|
|
+ s32 start_index = 0;
|
|
|
|
|
+
|
|
|
|
|
+ u64 alloc_map_index = ((u64)(bpf_get_current_pid_tgid() >> 32) << 32) | start_index;
|
|
|
|
|
+ u64 *start = (u64 *)bpf_map_lookup_elem(&proc_alloc_map, &alloc_map_index);
|
|
|
|
|
+ if (start == NULL || *start == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+// bpf_printk("start == NULL || *start == 0");
|
|
|
|
|
+ u64 current_start_addr = start_addr + (partition_size * current_cpu);
|
|
|
|
|
+ bpf_map_update_elem(&proc_alloc_map, &alloc_map_index, ¤t_start_addr, BPF_ANY);
|
|
|
|
|
+ return current_start_addr;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+// bpf_printk("else return");
|
|
|
|
|
+ return *start;
|
|
|
}
|
|
}
|
|
|
- s64 partition_size = (end_addr - start_addr) / trace_conf->total_cpus;
|
|
|
|
|
- u32 current_cpu = bpf_get_smp_processor_id();
|
|
|
|
|
- s32 start_index = 0;
|
|
|
|
|
- u64 *start = (u64 *)bpf_map_lookup_elem(&alloc_map, &start_index);
|
|
|
|
|
- if (start == NULL || *start == 0)
|
|
|
|
|
- {
|
|
|
|
|
- u64 current_start_addr = start_addr + (partition_size * current_cpu);
|
|
|
|
|
- bpf_map_update_elem(&alloc_map, &start_index, ¤t_start_addr, BPF_ANY);
|
|
|
|
|
- return current_start_addr;
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- return *start;
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static __always_inline u64 get_area_end(u64 start,u64 start_addr,u64 end_addr)
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/*static __always_inline u64 get_area_end(u64 start,u64 start_addr,u64 end_addr)
|
|
|
{
|
|
{
|
|
|
u32 k0 = 0;
|
|
u32 k0 = 0;
|
|
|
struct trace_conf_t *trace_conf = trace_conf_map__lookup(&k0);
|
|
struct trace_conf_t *trace_conf = trace_conf_map__lookup(&k0);
|
|
@@ -78,8 +94,33 @@ static __always_inline u64 get_area_end(u64 start,u64 start_addr,u64 end_addr)
|
|
|
{
|
|
{
|
|
|
return *end;
|
|
return *end;
|
|
|
}
|
|
}
|
|
|
|
|
+}*/
|
|
|
|
|
+
|
|
|
|
|
+static __always_inline u64 get_area_end(u64 start,u64 start_addr,u64 end_addr)
|
|
|
|
|
+{
|
|
|
|
|
+ u32 k0 = 0;
|
|
|
|
|
+ struct trace_conf_t *trace_conf = trace_conf_map__lookup(&k0);
|
|
|
|
|
+ if (!trace_conf) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ s64 partition_size = (end_addr - start_addr) / trace_conf->total_cpus;
|
|
|
|
|
+ s32 end_index = 1;
|
|
|
|
|
+ u64 alloc_map_index = ((u64)(bpf_get_current_pid_tgid() >> 32) << 32) | end_index;
|
|
|
|
|
+
|
|
|
|
|
+ u64 *end = (u64 *)bpf_map_lookup_elem(&proc_alloc_map, &alloc_map_index);
|
|
|
|
|
+ if (end == NULL || *end == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ u64 current_end_addr = start + partition_size;
|
|
|
|
|
+ bpf_map_update_elem(&proc_alloc_map, &alloc_map_index, ¤t_end_addr, BPF_ANY);
|
|
|
|
|
+ return current_end_addr;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ return *end;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
static __always_inline s32 bound_number(s32 num, s32 min, s32 max)
|
|
static __always_inline s32 bound_number(s32 num, s32 min, s32 max)
|
|
|
{
|
|
{
|
|
|
if (num < min)
|
|
if (num < min)
|
|
@@ -156,12 +197,80 @@ static __always_inline void *write_target_data(void *data, s32 size)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+//static __always_inline void *cw_write_target_data(void *data, s32 size, struct ebpf_proc_info* info)
|
|
|
|
|
+//{
|
|
|
|
|
+// if (!data || data == NULL)
|
|
|
|
|
+// {
|
|
|
|
|
+// bpf_printk("163 err");
|
|
|
|
|
+// return NULL;
|
|
|
|
|
+// }
|
|
|
|
|
+// u64 start_from_proc;
|
|
|
|
|
+// u64 end_from_proc;
|
|
|
|
|
+//// __u32 tgid = (__u32)(bpf_get_current_pid_tgid() >> 32);
|
|
|
|
|
+//// struct ebpf_proc_info *info = bpf_map_lookup_elem(&proc_info_map, &tgid);
|
|
|
|
|
+//// if (!info) {
|
|
|
|
|
+//// return NULL;
|
|
|
|
|
+//// }
|
|
|
|
|
+// start_from_proc = info->start_addr;
|
|
|
|
|
+// end_from_proc = info->end_addr;
|
|
|
|
|
+//
|
|
|
|
|
+// u64 start = get_area_start(start_from_proc, end_from_proc);
|
|
|
|
|
+// u64 end = get_area_end(start,start_from_proc,end_from_proc);
|
|
|
|
|
+// if (end - start < size)
|
|
|
|
|
+// {
|
|
|
|
|
+// cw_bpf_debug("reached end of CPU memory block, going to the start again");
|
|
|
|
|
+// s32 start_index = 0;
|
|
|
|
|
+// bpf_map_delete_elem(&alloc_map, &start_index);
|
|
|
|
|
+// start = get_area_start(start_from_proc, end_from_proc);
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// void *target = (void *)start;
|
|
|
|
|
+// size = bound_number(size, MIN_BUFFER_SIZE, MAX_BUFFER_SIZE);
|
|
|
|
|
+// u64 page_offset = (u64)target & 0xFFF;
|
|
|
|
|
+// u64 dist_to_next_page = 4096 - page_offset;
|
|
|
|
|
+// if (dist_to_next_page < size)
|
|
|
|
|
+// {
|
|
|
|
|
+// target += dist_to_next_page;
|
|
|
|
|
+// }
|
|
|
|
|
+// u64 target_u = (u64)target;
|
|
|
|
|
+// if (target_u > end_from_proc || target_u < start_from_proc) {
|
|
|
|
|
+// cw_bpf_debug("TARGET ADDRESS IS OUT OF BOUNDS: 0x%llx", target);
|
|
|
|
|
+//// bpf_printk("no target_u:%llu start:%llu end:%llu", target_u, start_from_proc, end_from_proc);
|
|
|
|
|
+// bpf_printk("197 err");
|
|
|
|
|
+// return NULL;
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// long success = bpf_probe_write_user(target, data, size);
|
|
|
|
|
+// if (success == 0)
|
|
|
|
|
+// {
|
|
|
|
|
+// s32 start_index = 0;
|
|
|
|
|
+// // Update the start position of this chunk, taking into account possible adjustments
|
|
|
|
|
+// // we made to be page aligned
|
|
|
|
|
+// u64 updated_start = target_u + size;
|
|
|
|
|
+//
|
|
|
|
|
+// // align updated_start to 8 bytes
|
|
|
|
|
+// if (updated_start % 8 != 0) {
|
|
|
|
|
+// updated_start += 8 - (updated_start % 8);
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// bpf_map_update_elem(&alloc_map, &start_index, &updated_start, BPF_ANY);
|
|
|
|
|
+// return target;
|
|
|
|
|
+// }
|
|
|
|
|
+// else
|
|
|
|
|
+// {
|
|
|
|
|
+// bpf_printk("failed to write to userspace, error code: %d, addr: %lx, size: %d", success, target, size);
|
|
|
|
|
+// return NULL;
|
|
|
|
|
+// }
|
|
|
|
|
+//}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
static __always_inline void *cw_write_target_data(void *data, s32 size, struct ebpf_proc_info* info)
|
|
static __always_inline void *cw_write_target_data(void *data, s32 size, struct ebpf_proc_info* info)
|
|
|
{
|
|
{
|
|
|
- if (!data || data == NULL)
|
|
|
|
|
- {
|
|
|
|
|
- return NULL;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (!data || data == NULL)
|
|
|
|
|
+ {
|
|
|
|
|
+ bpf_printk("163 err");
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
u64 start_from_proc;
|
|
u64 start_from_proc;
|
|
|
u64 end_from_proc;
|
|
u64 end_from_proc;
|
|
|
// __u32 tgid = (__u32)(bpf_get_current_pid_tgid() >> 32);
|
|
// __u32 tgid = (__u32)(bpf_get_current_pid_tgid() >> 32);
|
|
@@ -173,50 +282,53 @@ static __always_inline void *cw_write_target_data(void *data, s32 size, struct e
|
|
|
end_from_proc = info->end_addr;
|
|
end_from_proc = info->end_addr;
|
|
|
|
|
|
|
|
u64 start = get_area_start(start_from_proc, end_from_proc);
|
|
u64 start = get_area_start(start_from_proc, end_from_proc);
|
|
|
- u64 end = get_area_end(start,start_from_proc,end_from_proc);
|
|
|
|
|
- if (end - start < size)
|
|
|
|
|
- {
|
|
|
|
|
- cw_bpf_debug("reached end of CPU memory block, going to the start again");
|
|
|
|
|
- s32 start_index = 0;
|
|
|
|
|
- bpf_map_delete_elem(&alloc_map, &start_index);
|
|
|
|
|
- start = get_area_start(start_from_proc, end_from_proc);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void *target = (void *)start;
|
|
|
|
|
- size = bound_number(size, MIN_BUFFER_SIZE, MAX_BUFFER_SIZE);
|
|
|
|
|
- u64 page_offset = (u64)target & 0xFFF;
|
|
|
|
|
- u64 dist_to_next_page = 4096 - page_offset;
|
|
|
|
|
- if (dist_to_next_page < size)
|
|
|
|
|
- {
|
|
|
|
|
- target += dist_to_next_page;
|
|
|
|
|
- }
|
|
|
|
|
- u64 target_u = (u64)target;
|
|
|
|
|
- if (target_u > end_from_proc || target_u < start_from_proc) {
|
|
|
|
|
- cw_bpf_debug("TARGET ADDRESS IS OUT OF BOUNDS: 0x%llx", target);
|
|
|
|
|
- return NULL;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ u64 end = get_area_end(start,start_from_proc,end_from_proc);
|
|
|
|
|
+ s32 start_index = 0;
|
|
|
|
|
+ u64 alloc_map_index = ((u64)(bpf_get_current_pid_tgid() >> 32) << 32) | start_index;
|
|
|
|
|
+ if (end - start < size)
|
|
|
|
|
+ {
|
|
|
|
|
+ cw_bpf_debug("reached end of CPU memory block, going to the start again");
|
|
|
|
|
+ bpf_map_delete_elem(&proc_alloc_map, &alloc_map_index);
|
|
|
|
|
+ start = get_area_start(start_from_proc, end_from_proc);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- long success = bpf_probe_write_user(target, data, size);
|
|
|
|
|
- if (success == 0)
|
|
|
|
|
- {
|
|
|
|
|
- s32 start_index = 0;
|
|
|
|
|
- // Update the start position of this chunk, taking into account possible adjustments
|
|
|
|
|
- // we made to be page aligned
|
|
|
|
|
- u64 updated_start = target_u + size;
|
|
|
|
|
|
|
+ void *target = (void *)start;
|
|
|
|
|
+ size = bound_number(size, MIN_BUFFER_SIZE, MAX_BUFFER_SIZE);
|
|
|
|
|
+ u64 page_offset = (u64)target & 0xFFF;
|
|
|
|
|
+ u64 dist_to_next_page = 4096 - page_offset;
|
|
|
|
|
+ if (dist_to_next_page < size)
|
|
|
|
|
+ {
|
|
|
|
|
+ target += dist_to_next_page;
|
|
|
|
|
+ }
|
|
|
|
|
+ u64 target_u = (u64)target;
|
|
|
|
|
+ if (target_u > end_from_proc || target_u < start_from_proc) {
|
|
|
|
|
+ cw_bpf_debug("TARGET ADDRESS IS OUT OF BOUNDS: 0x%llx", target);
|
|
|
|
|
+// bpf_printk("no target_u:%llu start:%llu end:%llu", target_u, start_from_proc, end_from_proc);
|
|
|
|
|
+ bpf_printk("197 err");
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+// bpf_printk("ok target_u:%llu start:%llu end:%llu", target_u, start, end);
|
|
|
|
|
|
|
|
- // align updated_start to 8 bytes
|
|
|
|
|
- if (updated_start % 8 != 0) {
|
|
|
|
|
- updated_start += 8 - (updated_start % 8);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ long success = bpf_probe_write_user(target, data, size);
|
|
|
|
|
+ if (success == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+// s32 start_index = 0;
|
|
|
|
|
+ // Update the start position of this chunk, taking into account possible adjustments
|
|
|
|
|
+ // we made to be page aligned
|
|
|
|
|
+ u64 updated_start = target_u + size;
|
|
|
|
|
|
|
|
- bpf_map_update_elem(&alloc_map, &start_index, &updated_start, BPF_ANY);
|
|
|
|
|
- return target;
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- bpf_printk("failed to write to userspace, error code: %d, addr: %lx, size: %d", success, target, size);
|
|
|
|
|
- return NULL;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // align updated_start to 8 bytes
|
|
|
|
|
+ if (updated_start % 8 != 0) {
|
|
|
|
|
+ updated_start += 8 - (updated_start % 8);
|
|
|
|
|
+ }
|
|
|
|
|
+ bpf_map_update_elem(&proc_alloc_map, &alloc_map_index, &updated_start, BPF_ANY);
|
|
|
|
|
+ return target;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ bpf_printk("failed to write to userspace, error code: %d, addr: %lx, size: %d", success, target, size);
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
#endif
|