summaryrefslogtreecommitdiff
path: root/writer/swap_writer_module.c
blob: 8bc2c7e626f514c343d49466bbdc8bc8fbffd29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
 *  SWAP Writer
 *  modules/writer/swap_writer_module.c
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) Samsung Electronics, 2013
 *
 * 2013	 Alexander Aksenov <a.aksenov@samsung.com>, Vyacheslav Cherkashin: 
 *                  SWAP Writer module implement
 *
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/dcache.h>
#include <linux/mm.h>
#include <linux/mm_types.h>
#include <linux/fs.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <asm/uaccess.h>

#include "../buffer/swap_buffer_module.h"
#include "../buffer/swap_buffer_errors.h"

#include "swap_writer_module.h"
#include "swap_writer_errors.h"
#include "kernel_operations.h"


enum MSG_ID {
	MSG_PROC_INFO			= 0x2002,
	MSG_ERROR			= 0x2005,
	MSG_SAMPLE			= 0x2006,
	MSG_FUNCTION_ENTRY		= 0x2010,
	MSG_FUNCTION_EXIT		= 0x2011,
	MSG_CONTEXT_SWITCH_ENTRY	= 0x2012,
	MSG_CONTEXT_SWITCH_EXIT		= 0x2013
};

static char *cpu_buf[NR_CPUS];
static u32 seq_num = 0;
static unsigned int discarded = 0;

int init_msg(size_t buf_size)
{
	int i;

	for (i = 0; i < NR_CPUS; ++i)
		cpu_buf[i] = kmalloc(buf_size, GFP_KERNEL);

	return E_SW_SUCCESS;
}
EXPORT_SYMBOL_GPL(init_msg);

void uninit_msg(void)
{
	int i;

	for (i = 0; i < NR_CPUS; ++i)
		kfree(cpu_buf[i]);
}
EXPORT_SYMBOL_GPL(uninit_msg);

void reset_discarded(void)
{
	discarded = 0;
}
EXPORT_SYMBOL_GPL(reset_discarded);

void reset_seq_num(void)
{
	seq_num = 0;
}
EXPORT_SYMBOL_GPL(reset_seq_num);

unsigned int get_discarded_count(void)
{
	return discarded;
}
EXPORT_SYMBOL_GPL(get_discarded_count);

static char *get_current_buf(void)
{
	return cpu_buf[task_cpu(current)];
}

static inline u64 timespec2time(struct timespec *ts)
{
	return ((u64)ts->tv_nsec) << 32 | ts->tv_sec;
}

/* ============================================================================
 * =                         BASIC MESSAGE FORMAT                             =
 * ============================================================================
 */

struct basic_msg_fmt {
	u32 msg_id;
	u32 seq_number;
	u64 time;
	u32 len;
	char payload[0];
} __attribute__((packed));

#if 0 /* debug */
static void print_hex(char *ptr, int len)
{
	int i;

	printk("print_hex:\n");
	for (i = 0; i < len; ++i) {
		printk("[%x]  [%3d]=%2x\n", &ptr[i], i, ptr[i]);
	}
}
#endif

static int write_to_buffer(void *data)
{
	int result;
	struct basic_msg_fmt *bmf = (struct basic_msg_fmt *)data;

	result = swap_buffer_write(bmf, bmf->len + sizeof(*bmf));
	if (result < 0) {
		discarded++;
	}

	return result;
}

static void set_len_msg(char *buf, char *end)
{
	struct basic_msg_fmt *bmf = (struct basic_msg_fmt *)buf;
	bmf->len = end - buf - sizeof(*bmf);
}

static inline void set_seq_num(struct basic_msg_fmt *bmf)
{
	bmf->seq_number = seq_num;
	seq_num++;
}

static inline void set_time(struct basic_msg_fmt *bmf)
{
	struct timespec ts;

	getnstimeofday(&ts);
	bmf->time = timespec2time(&ts);
}

static char* pack_basic_msg_fmt(char *buf, enum MSG_ID id)
{
	struct basic_msg_fmt *bmf = (struct basic_msg_fmt *)buf;

	set_time(bmf);
	set_seq_num(bmf);
	bmf->msg_id = id;

	return bmf->payload;
}





/* ============================================================================
 * =                             PROCESS INFO                                 =
 * ============================================================================
 */

struct proc_info {
	u32 pid;
	u64 start_time;
	u64 low_addr;
	u64 high_addr;
	u32 app_type;
	u32 bin_type;
	char bin_path[0];
} __attribute__((packed));

struct proc_info_part {
	u32 lib_cnt;
	char libs[0];
} __attribute__((packed));

struct lib_obj {
	u64 low_addr;
	u64 high_addr;
	char lib_path[0];
} __attribute__((packed));

static char *pack_path(char *buf, struct file *file)
{
	enum { TMP_BUF_LEN = 512 };
	char tmp_buf[TMP_BUF_LEN];
	char NA[] = "N/A";
	char *filename;
	size_t len;

	if (file == NULL)
		return strcpy(buf, NA) + sizeof(NA);

	filename = d_path(&file->f_path, tmp_buf, TMP_BUF_LEN);
	if (IS_ERR_OR_NULL(filename))
		return strcpy(buf, NA) + sizeof(NA);

	len = strlen(filename) + 1;
	memcpy(buf, filename, len);

	return buf + len;
}

static char *pack_lib_obj(char *lib_obj, struct vm_area_struct *vma)
{
	struct lib_obj *lo = (struct lib_obj *)lib_obj;
	struct file *file;

	lo->low_addr = vma->vm_start;
	lo->high_addr = vma->vm_end;

	return pack_path(lo->lib_path, vma->vm_file);
}

/* FIXME: check_vma()*/
static int check_vma(struct vm_area_struct *vma)
{
	return vma->vm_file && !(vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || (vma->vm_flags & VM_ACCOUNT) ||
			!(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) ||
			!(vma->vm_flags & (VM_READ | VM_MAYREAD)));
}

static struct vm_area_struct *find_vma_by_dentry(struct mm_struct *mm,
					  struct dentry *dentry)
{
	struct vm_area_struct *vma;

	down_write(&mm->mmap_sem);
	for (vma = mm->mmap; vma; vma = vma->vm_next) {
		if (check_vma(vma) && vma->vm_file &&
		    (vma->vm_file->f_dentry == dentry))
			goto out;
	}

	vma = NULL;

out:
	up_write(&mm->mmap_sem);

	return vma;
}

static char *pack_proc_info_part(char *bin_path, struct mm_struct *mm)
{
	struct proc_info_part *pip;
	struct vm_area_struct *vma;
	char *lib_obj, *end_path = NULL;
	int lib_cnt = 0;
	char bin_path_def[] = "";

	memcpy(bin_path, bin_path_def, sizeof(bin_path_def));
	end_path = bin_path + sizeof(bin_path_def);

	pip = (struct proc_info_part *)end_path;
	lib_obj = pip->libs;

	down_write(&mm->mmap_sem);
	for (vma = mm->mmap; vma; vma = vma->vm_next) {
		if (check_vma(vma)) {
			lib_obj = pack_lib_obj(lib_obj, vma);
			++lib_cnt;
		}
	}
	up_write(&mm->mmap_sem);

	pip->lib_cnt = lib_cnt;
	return lib_obj;
}

static char *pack_proc_info(char *payload, struct task_struct *task,
			    void *priv)
{
	struct proc_info *pi = (struct proc_info *)payload;
	struct dentry *dentry_exec = (struct dentry *)priv;
	struct vm_area_struct *vma = find_vma_by_dentry(task->mm, dentry_exec);

	pi->pid = task->tgid;

	/* FIXME: */
	pi->start_time = timespec2time(&task->start_time);
	pi->low_addr = vma ? vma->vm_start : 0;
	pi->high_addr = vma ? vma->vm_end : 0;
	pi->app_type = 1;	/* TODO: hardcode for Tizen*/
	pi->bin_type = 0;	/* TODO: determined in US */

	return pack_proc_info_part(pi->bin_path, task->mm);
}

int proc_info_msg(struct task_struct *task, void *priv)
{
	char *buf, *payload, *buf_end;

	buf = get_current_buf();
	payload = pack_basic_msg_fmt(buf, MSG_PROC_INFO);
	buf_end = pack_proc_info(payload, task, priv);

	set_len_msg(buf, buf_end);

	return write_to_buffer(buf);
}
EXPORT_SYMBOL_GPL(proc_info_msg);




/* ============================================================================
 * =                                 SAMPLE                                   =
 * ============================================================================
 */

struct sample {
	u32 pid;
	u64 pc_addr;
	u32 tid;
	u32 cpu_num;
} __attribute__((packed));

static char *pack_sample(char *payload, struct pt_regs *regs)
{
	struct sample *s = (struct sample *)payload;
	struct task_struct *task = current;

	s->pid = task->tgid;
	s->pc_addr = get_regs_ip(regs);
	s->tid = task->pid;
	s->cpu_num = task_cpu(current);

	return payload + sizeof(*s);
}

int sample_msg(struct pt_regs *regs)
{
	char *buf, *payload, *buf_end;

	buf = get_current_buf();
	payload = pack_basic_msg_fmt(buf, MSG_SAMPLE);
	buf_end = pack_sample(payload, regs);

	set_len_msg(buf, buf_end);

	return write_to_buffer(buf);
}
EXPORT_SYMBOL_GPL(sample_msg);




/* ============================================================================
 * =                                 ENTRY                                    =
 * ============================================================================
 */

struct msg_func_entry {
	u64 pc_addr;
	u64 caller_pc_addr;
	u16 probe_type;
	u16 probe_sub_type;
	u32 pid;
	u32 tid;
	u32 cpu_num;
	u32 cnt_args;
	char args[0];
} __attribute__((packed));

static char *pack_msg_func_entry(char *payload, const char *fmt, struct pt_regs *regs,
				 enum PROBE_TYPE pt, int sub_type)
{
	struct msg_func_entry *mfe = (struct msg_func_entry *)payload;
	struct task_struct *task = current;

	mfe->pid = task->tgid;
	mfe->tid = task->pid;
	mfe->cpu_num = task_cpu(task);
	mfe->pc_addr = get_regs_ip(regs);
//TODO ret address for x86!
	mfe->caller_pc_addr = get_regs_ret_func(regs);
	mfe->probe_type = pt;
	mfe->probe_sub_type = sub_type;
	mfe->cnt_args = strlen(fmt);

	return payload + sizeof(*mfe);
}

static int pack_args(char *buf, int len, const char *fmt, struct pt_regs *regs)
{
	enum { args_cnt = 16 };
	char *buf_old = buf;
	unsigned long arg, args[args_cnt];
	u32 *tmp_u32;
	u64 *tmp_u64;
	int i, cnt;

	cnt = strlen(fmt);

	/* FIXME: when the number of arguments is greater than args_cnt */
	cnt = cnt < args_cnt ? cnt : args_cnt;
	get_args(args, cnt, regs);

	for (i = 0; i < cnt; ++i) {
		if (len < 2)
			return -ENOMEM;

		arg = args[i];
		*buf = fmt[i];
		buf += 1;
		len -= 1;

		switch (fmt[i]) {
		case 'c': /* 1 byte(char) */
			if (len < 1)
				return -ENOMEM;
			*buf = (char)arg;
			buf += 1;
			len -= 1;
			break;

		case 'd': /* 4 byte(int) */
			if (len < 4)
				return -ENOMEM;
			tmp_u32 = buf;
			*tmp_u32 = arg;
			buf += 4;
			len -= 4;
			break;

		case 'x': /* 8 byte(long) */
		case 'p': /* 8 byte(pointer) */
			if (len < 8)
				return -ENOMEM;
			tmp_u64 = buf;
			*tmp_u64 = (u64)arg;
			buf += 8;
			len -= 8;
			break;
//		case 'f': /* 4 byte(float) */
//			break;
//		case 'w': /* 8 byte(double) */
//			break;
		case 's': /* string end with '\0' */
		{
			enum { max_str_len = 512 };
			const char __user *user_s;
			int len_s, ret;

			user_s = (const char __user *)arg;
			len_s = strnlen_user(user_s, max_str_len);
			if (len < len_s)
				return -ENOMEM;

			ret = strncpy_from_user(buf, user_s, len_s);
			if (ret < 0)
				return -EFAULT;

			buf += ret + 1;
			len -= ret + 1;
		}
			break;
		default:
			return -EINVAL;
		}
	}

	return buf - buf_old;
}

int entry_event(const char *fmt, struct pt_regs *regs,
		 enum PROBE_TYPE pt, int sub_type)
{
	char *buf, *payload, *args, *buf_end;
	int ret;

	buf = get_current_buf();
	payload = pack_basic_msg_fmt(buf, MSG_FUNCTION_ENTRY);
	args = pack_msg_func_entry(payload, fmt, regs, pt, sub_type);

	/* FIXME: len = 1024 */
	ret = pack_args(args, 1024, fmt, regs);
	if (ret < 0) {
		printk("ERROR: !!!!!\n");
		return ret;
	}

	buf_end = args + ret;

	set_len_msg(buf, buf_end);

	return write_to_buffer(buf);
}
EXPORT_SYMBOL_GPL(entry_event);





/* ============================================================================
 * =                                 EXIT                                     =
 * ============================================================================
 */

struct msg_func_exit {
	u32 pid;
	u32 tid;
	u64 pc_addr;
	u32 cpu_num;
	u64 ret_val;
} __attribute__((packed));

static char *pack_msg_func_exit(char *payload, struct pt_regs *regs)
{
	struct msg_func_exit *mfe = (struct msg_func_exit *)payload;
	struct task_struct *task = current;

	mfe->pid = task->tgid;
	mfe->tid = task->pid;
	mfe->cpu_num = task_cpu(task);
	mfe->pc_addr = get_regs_ip(regs);
//TODO x86
	mfe->ret_val = get_regs_ret_val(regs);

	return payload + sizeof(*mfe);
}

int exit_event(struct pt_regs *regs)
{
	char *buf, *payload, *buf_end;

	buf = get_current_buf();
	payload = pack_basic_msg_fmt(buf, MSG_FUNCTION_EXIT);
	buf_end = pack_msg_func_exit(payload, regs);
	set_len_msg(buf, buf_end);

	return write_to_buffer(buf);
}
EXPORT_SYMBOL_GPL(exit_event);





/* ============================================================================
 * =                            CONTEXT SWITCH                                =
 * ============================================================================
 */

struct msg_context_switch {
	u64 pc_addr;
	u32 pid;
	u32 tid;
	u32 cpu_num;
} __attribute__((packed));

static char *pack_msg_context_switch(char *payload, struct pt_regs *regs)
{
	struct msg_context_switch *mcs = (struct msg_context_switch *)payload;
	struct task_struct *task = current;

	mcs->pc_addr = 0;
	mcs->pid = task->tgid;
	mcs->tid = task->pid;
	mcs->cpu_num = task_cpu(task);

	return payload + sizeof(*mcs);
}

static int context_switch(struct pt_regs *regs, enum MSG_ID id)
{
	char *buf, *payload, *buf_end;

	buf = get_current_buf();
	payload = pack_basic_msg_fmt(buf, id);
	buf_end = pack_msg_context_switch(payload, regs);
	set_len_msg(buf, buf_end);

	return write_to_buffer(buf);
}

int switch_entry(struct pt_regs *regs)
{
	return context_switch(regs, MSG_CONTEXT_SWITCH_ENTRY);
}
EXPORT_SYMBOL_GPL(switch_entry);

int switch_exit(struct pt_regs *regs)
{
	return context_switch(regs, MSG_CONTEXT_SWITCH_EXIT);
}
EXPORT_SYMBOL_GPL(switch_exit);




/* ============================================================================
 * =                                 ERROR                                    =
 * ============================================================================
 */

struct msg_err {
	char msg[0];
} __attribute__((packed));

static char *pack_msg_err(char *payload, const char *fmt, va_list args)
{
	struct msg_err *me = (struct msg_err *)payload;
	int ret;

	ret = vsprintf(me->msg, fmt, args);
	if (ret < 0)
		return payload;

	return payload + sizeof(*me) + ret + 1;
}

int error_msg(const char *fmt, ...)
{
	char *buf, *payload, *buf_end;
	va_list args;

	buf = get_current_buf();
	payload = pack_basic_msg_fmt(buf, MSG_ERROR);

	va_start(args, fmt);
	buf_end = pack_msg_err(payload, fmt, args);
	va_end(args);

	set_len_msg(buf, buf_end);

	return write_to_buffer(buf);
}
EXPORT_SYMBOL_GPL(error_msg);

/* ============================================================================
 * =                         MESSAGES FROM USER SPACE                         =
 * ============================================================================
 */

int us_msg(void *us_message)
{
	struct basic_msg_fmt *bmf = (struct basic_msg_fmt *)us_message;

	set_seq_num(bmf);
	return write_to_buffer(us_message);
}
EXPORT_SYMBOL_GPL(us_msg);


static int __init swap_writer_module_init(void)
{
	print_msg("SWAP Writer initialized\n");

	return 0;
}

static void __exit swap_writer_module_exit(void)
{
	print_msg("SWAP Writer uninitialized\n");
}




module_init(swap_writer_module_init);
module_exit(swap_writer_module_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SWAP Writer module");
MODULE_AUTHOR("Cherkashin V., Aksenov A.S.");