1 /**
2 * Copyright (C) ARM Limited 2010-2011. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 */
10 #define COOKIEMAP_ENTRIES 1024 /* must be power of 2 */
11 #define TRANSLATE_SIZE 256
12 #define MAX_COLLISIONS 2
14 static uint32_t *gator_crc32_table;
15 static uint32_t translate_buffer_mask;
17 static DEFINE_PER_CPU(char *, translate_text);
18 static DEFINE_PER_CPU(uint32_t, cookie_next_key);
19 static DEFINE_PER_CPU(uint64_t *, cookie_keys);
20 static DEFINE_PER_CPU(uint32_t *, cookie_values);
21 static DEFINE_PER_CPU(int, translate_buffer_read);
22 static DEFINE_PER_CPU(int, translate_buffer_write);
23 static DEFINE_PER_CPU(unsigned int *, translate_buffer);
25 static inline uint32_t get_cookie(int cpu, int buftype, struct task_struct *task, struct vm_area_struct *vma, struct module *mod, bool in_interrupt);
26 static void wq_cookie_handler(struct work_struct *unused);
27 DECLARE_WORK(cookie_work, wq_cookie_handler);
29 static uint32_t cookiemap_code(uint64_t value64) {
30 uint32_t value = (uint32_t)((value64 >> 32) + value64);
31 uint32_t cookiecode = (value >> 24) & 0xff;
32 cookiecode = cookiecode * 31 + ((value >> 16) & 0xff);
33 cookiecode = cookiecode * 31 + ((value >> 8) & 0xff);
34 cookiecode = cookiecode * 31 + ((value >> 0) & 0xff);
35 cookiecode &= (COOKIEMAP_ENTRIES-1);
36 return cookiecode * MAX_COLLISIONS;
37 }
39 static uint32_t gator_chksum_crc32(char *data)
40 {
41 register unsigned long crc;
42 unsigned char *block = data;
43 int i, length = strlen(data);
45 crc = 0xFFFFFFFF;
46 for (i = 0; i < length; i++) {
47 crc = ((crc >> 8) & 0x00FFFFFF) ^ gator_crc32_table[(crc ^ *block++) & 0xFF];
48 }
50 return (crc ^ 0xFFFFFFFF);
51 }
53 /*
54 * Exists
55 * Pre: [0][1][v][3]..[n-1]
56 * Post: [v][0][1][3]..[n-1]
57 */
58 static uint32_t cookiemap_exists(uint64_t key) {
59 unsigned long x, flags, retval = 0;
60 int cpu = smp_processor_id();
61 uint32_t cookiecode = cookiemap_code(key);
62 uint64_t *keys = &(per_cpu(cookie_keys, cpu)[cookiecode]);
63 uint32_t *values = &(per_cpu(cookie_values, cpu)[cookiecode]);
65 // Can be called from interrupt handler or from work queue
66 local_irq_save(flags);
67 for (x = 0; x < MAX_COLLISIONS; x++) {
68 if (keys[x] == key) {
69 uint32_t value = values[x];
70 for (; x > 0; x--) {
71 keys[x] = keys[x-1];
72 values[x] = values[x-1];
73 }
74 keys[0] = key;
75 values[0] = value;
76 retval = value;
77 break;
78 }
79 }
80 local_irq_restore(flags);
82 return retval;
83 }
85 /*
86 * Add
87 * Pre: [0][1][2][3]..[n-1]
88 * Post: [v][0][1][2]..[n-2]
89 */
90 static void cookiemap_add(uint64_t key, uint32_t value) {
91 int cpu = smp_processor_id();
92 int cookiecode = cookiemap_code(key);
93 uint64_t *keys = &(per_cpu(cookie_keys, cpu)[cookiecode]);
94 uint32_t *values = &(per_cpu(cookie_values, cpu)[cookiecode]);
95 int x;
97 for (x = MAX_COLLISIONS-1; x > 0; x--) {
98 keys[x] = keys[x-1];
99 values[x] = values[x-1];
100 }
101 keys[0] = key;
102 values[0] = value;
103 }
105 static void translate_buffer_write_int(int cpu, unsigned int x)
106 {
107 per_cpu(translate_buffer, cpu)[per_cpu(translate_buffer_write, cpu)++] = x;
108 per_cpu(translate_buffer_write, cpu) &= translate_buffer_mask;
109 }
111 static unsigned int translate_buffer_read_int(int cpu)
112 {
113 unsigned int value = per_cpu(translate_buffer, cpu)[per_cpu(translate_buffer_read, cpu)++];
114 per_cpu(translate_buffer_read, cpu) &= translate_buffer_mask;
115 return value;
116 }
118 static void wq_cookie_handler(struct work_struct *unused)
119 {
120 struct task_struct *task;
121 struct vm_area_struct *vma;
122 int cpu = smp_processor_id();
123 unsigned int cookie, commit;
125 commit = per_cpu(translate_buffer_write, cpu);
126 while (per_cpu(translate_buffer_read, cpu) != commit) {
127 task = (struct task_struct *)translate_buffer_read_int(cpu);
128 vma = (struct vm_area_struct *)translate_buffer_read_int(cpu);
129 cookie = get_cookie(cpu, TIMER_BUF, task, vma, NULL, false);
130 }
131 }
133 // Retrieve full name from proc/pid/cmdline for java processes on Android
134 static int translate_app_process(char** text, int cpu, struct task_struct * task, struct vm_area_struct *vma, bool in_interrupt)
135 {
136 void *maddr;
137 unsigned int len;
138 unsigned long addr;
139 struct mm_struct *mm;
140 struct page *page = NULL;
141 struct vm_area_struct *page_vma;
142 int bytes, offset, retval = 0, ptr;
143 char * buf = per_cpu(translate_text, cpu);
145 // Push work into a work queue if in atomic context as the kernel functions below might sleep
146 // Rely on the in_interrupt variable rather than in_irq() or in_interrupt() kernel functions, as the value of these functions seems
147 // inconsistent during a context switch between android/linux versions
148 if (in_interrupt) {
149 // Check if already in buffer
150 ptr = per_cpu(translate_buffer_read, cpu);
151 while (ptr != per_cpu(translate_buffer_write, cpu)) {
152 if (per_cpu(translate_buffer, cpu)[ptr] == (int)task)
153 goto out;
154 ptr = (ptr + 2) & translate_buffer_mask;
155 }
157 translate_buffer_write_int(cpu, (unsigned int)task);
158 translate_buffer_write_int(cpu, (unsigned int)vma);
159 schedule_work(&cookie_work);
160 goto out;
161 }
163 mm = get_task_mm(task);
164 if (!mm)
165 goto out;
166 if (!mm->arg_end)
167 goto outmm;
168 addr = mm->arg_start;
169 len = mm->arg_end - mm->arg_start;
171 if (len > TRANSLATE_SIZE)
172 len = TRANSLATE_SIZE;
174 down_read(&mm->mmap_sem);
175 while (len) {
176 if (get_user_pages(task, mm, addr, 1, 0, 1, &page, &page_vma) <= 0)
177 goto outsem;
179 maddr = kmap(page);
180 offset = addr & (PAGE_SIZE-1);
181 bytes = len;
182 if (bytes > PAGE_SIZE - offset)
183 bytes = PAGE_SIZE - offset;
185 copy_from_user_page(page_vma, page, addr, buf, maddr + offset, bytes);
187 kunmap(page); // release page allocated by get_user_pages()
188 page_cache_release(page);
190 len -= bytes;
191 buf += bytes;
192 addr += bytes;
194 *text = per_cpu(translate_text, cpu);
195 retval = 1;
196 }
198 // On app_process startup, /proc/pid/cmdline is initially "zygote" then "<pre-initialized>" but changes after an initial startup period
199 if (strcmp(*text, "zygote") == 0 || strcmp(*text, "<pre-initialized>") == 0)
200 retval = 0;
202 outsem:
203 up_read(&mm->mmap_sem);
204 outmm:
205 mmput(mm);
206 out:
207 return retval;
208 }
210 static inline uint32_t get_cookie(int cpu, int buftype, struct task_struct *task, struct vm_area_struct *vma, struct module *mod, bool in_interrupt)
211 {
212 unsigned long flags, cookie;
213 struct path *path;
214 uint64_t key;
215 char *text;
217 if (mod) {
218 text = mod->name;
219 } else {
220 if (!vma || !vma->vm_file) {
221 return INVALID_COOKIE;
222 }
223 path = &vma->vm_file->f_path;
224 if (!path || !path->dentry) {
225 return INVALID_COOKIE;
226 }
228 text = (char*)path->dentry->d_name.name;
229 }
231 key = gator_chksum_crc32(text);
232 key = (key << 32) | (uint32_t)task->tgid;
234 cookie = cookiemap_exists(key);
235 if (cookie) {
236 return cookie;
237 }
239 if (strcmp(text, "app_process") == 0 && !mod) {
240 if (!translate_app_process(&text, cpu, task, vma, in_interrupt))
241 return INVALID_COOKIE;
242 }
244 // Can be called from interrupt handler or from work queue or from scheduler trace
245 local_irq_save(flags);
247 cookie = per_cpu(cookie_next_key, cpu)+=nr_cpu_ids;
248 cookiemap_add(key, cookie);
250 gator_buffer_write_packed_int(cpu, buftype, MESSAGE_COOKIE);
251 gator_buffer_write_packed_int(cpu, buftype, cookie);
252 gator_buffer_write_string(cpu, buftype, text);
254 local_irq_restore(flags);
256 return cookie;
257 }
259 static int get_exec_cookie(int cpu, int buftype, struct task_struct *task)
260 {
261 unsigned long cookie = NO_COOKIE;
262 struct mm_struct *mm = task->mm;
263 struct vm_area_struct *vma;
265 if (!mm)
266 return cookie;
268 for (vma = mm->mmap; vma; vma = vma->vm_next) {
269 if (!vma->vm_file)
270 continue;
271 if (!(vma->vm_flags & VM_EXECUTABLE))
272 continue;
273 cookie = get_cookie(cpu, buftype, task, vma, NULL, true);
274 break;
275 }
277 return cookie;
278 }
280 static unsigned long get_address_cookie(int cpu, int buftype, struct task_struct *task, unsigned long addr, off_t *offset)
281 {
282 unsigned long cookie = NO_COOKIE;
283 struct mm_struct *mm = task->mm;
284 struct vm_area_struct *vma;
286 if (!mm)
287 return cookie;
289 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
290 if (addr < vma->vm_start || addr >= vma->vm_end)
291 continue;
293 if (vma->vm_file) {
294 cookie = get_cookie(cpu, buftype, task, vma, NULL, true);
295 *offset = (vma->vm_pgoff << PAGE_SHIFT) + addr - vma->vm_start;
296 } else {
297 /* must be an anonymous map */
298 *offset = addr;
299 }
301 break;
302 }
304 if (!vma)
305 cookie = INVALID_COOKIE;
307 return cookie;
308 }
310 static int cookies_initialize(void)
311 {
312 uint32_t crc, poly;
313 int i, j, cpu, size, err = 0;
315 int translate_buffer_size = 512; // must be a power of 2
316 translate_buffer_mask = translate_buffer_size / sizeof(per_cpu(translate_buffer, 0)[0]) - 1;
318 for_each_present_cpu(cpu) {
319 per_cpu(cookie_next_key, cpu) = nr_cpu_ids + cpu;
321 size = COOKIEMAP_ENTRIES * MAX_COLLISIONS * sizeof(uint64_t);
322 per_cpu(cookie_keys, cpu) = (uint64_t*)kmalloc(size, GFP_KERNEL);
323 if (!per_cpu(cookie_keys, cpu)) {
324 err = -ENOMEM;
325 goto cookie_setup_error;
326 }
327 memset(per_cpu(cookie_keys, cpu), 0, size);
329 size = COOKIEMAP_ENTRIES * MAX_COLLISIONS * sizeof(uint32_t);
330 per_cpu(cookie_values, cpu) = (uint32_t*)kmalloc(size, GFP_KERNEL);
331 if (!per_cpu(cookie_values, cpu)) {
332 err = -ENOMEM;
333 goto cookie_setup_error;
334 }
335 memset(per_cpu(cookie_values, cpu), 0, size);
337 per_cpu(translate_buffer, cpu) = (unsigned int *)kmalloc(translate_buffer_size, GFP_KERNEL);
338 if (!per_cpu(translate_buffer, cpu)) {
339 err = -ENOMEM;
340 goto cookie_setup_error;
341 }
343 per_cpu(translate_buffer_write, cpu) = 0;
344 per_cpu(translate_buffer_read, cpu) = 0;
346 per_cpu(translate_text, cpu) = (char *)kmalloc(TRANSLATE_SIZE, GFP_KERNEL);
347 if (!per_cpu(translate_text, cpu)) {
348 err = -ENOMEM;
349 goto cookie_setup_error;
350 }
351 }
353 // build CRC32 table
354 poly = 0x04c11db7;
355 gator_crc32_table = (uint32_t*)kmalloc(256 * sizeof(uint32_t), GFP_KERNEL);
356 for (i = 0; i < 256; i++) {
357 crc = i;
358 for (j = 8; j > 0; j--) {
359 if (crc & 1) {
360 crc = (crc >> 1) ^ poly;
361 } else {
362 crc >>= 1;
363 }
364 }
365 gator_crc32_table[i] = crc;
366 }
368 cookie_setup_error:
369 return err;
370 }
372 static void cookies_release(void)
373 {
374 int cpu;
376 for_each_present_cpu(cpu) {
377 kfree(per_cpu(cookie_keys, cpu));
378 per_cpu(cookie_keys, cpu) = NULL;
380 kfree(per_cpu(cookie_values, cpu));
381 per_cpu(cookie_values, cpu) = NULL;
383 kfree(per_cpu(translate_buffer, cpu));
384 per_cpu(translate_buffer, cpu) = NULL;
385 per_cpu(translate_buffer_read, cpu) = 0;
386 per_cpu(translate_buffer_write, cpu) = 0;
388 kfree(per_cpu(translate_text, cpu));
389 per_cpu(translate_text, cpu) = NULL;
390 }
392 kfree(gator_crc32_table);
393 gator_crc32_table = NULL;
394 }