[FIX] Kprobe: remove aggrigated probe from list
[kernel/swap-modules.git] / parser / swap_msg_parser.c
1 /*
2  *  SWAP Parser
3  *  modules/parser/swap_msg_parser.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013  Vyacheslav Cherkashin, Vitaliy Cherepanov: SWAP Parser implement
22  *
23  */
24
25
26 #include <linux/module.h>
27 #include <linux/vmalloc.h>
28 #include <linux/cpumask.h>
29 #include <asm/uaccess.h>
30
31 #include "parser_defs.h"
32 #include "msg_buf.h"
33 #include "msg_cmd.h"
34 #include "cpu_ctrl.h"
35
36 #include <driver/driver_to_msg.h>
37 #include <driver/swap_ioctl.h>
38
39 enum MSG_ID {
40         MSG_KEEP_ALIVE          = 0x0001,
41         MSG_START               = 0x0002,
42         MSG_STOP                = 0x0003,
43         MSG_CONFIG              = 0x0004,
44         MSG_SWAP_INST_ADD       = 0x0008,
45         MSG_SWAP_INST_REMOVE    = 0x0009
46 };
47
48 struct basic_msg_fmt {
49         u32 msg_id;
50         u32 len;
51 } __attribute__((packed));
52
53 static int msg_handler(void __user *msg)
54 {
55         int ret;
56         u32 size;
57         enum MSG_ID msg_id;
58         struct msg_buf mb;
59         void __user *payload;
60         struct basic_msg_fmt bmf;
61         enum { size_max = 128 * 1024 * 1024 };
62
63         ret = copy_from_user(&bmf, (void*)msg, sizeof(bmf));
64         if (ret)
65                 return ret;
66
67         size = bmf.len;
68         if (size >= size_max) {
69                 printk("%s: too large message, size=%u\n", __func__, size);
70                 return -ENOMEM;
71         }
72
73         ret = init_mb(&mb, size);
74         if (ret)
75                 return ret;
76
77         payload = msg + sizeof(bmf);
78         if (size) {
79                 ret = copy_from_user(mb.begin, (void*)payload, size);
80                 if (ret)
81                         goto uninit;
82         }
83
84         msg_id = bmf.msg_id;
85         switch (msg_id) {
86         case MSG_KEEP_ALIVE:
87                 print_parse_debug("MSG_KEEP_ALIVE. size=%d\n", size);
88                 ret = msg_keep_alive(&mb);
89                 break;
90         case MSG_START:
91                 print_parse_debug("MSG_START. size=%d\n", size);
92                 ret = msg_start(&mb);
93                 break;
94         case MSG_STOP: {
95                 struct cpumask mask;
96
97                 print_parse_debug("MSG_STOP. size=%d\n", size);
98
99                 swap_disable_nonboot_cpus_lock(&mask);
100                 ret = msg_stop(&mb);
101                 swap_enable_nonboot_cpus_unlock(&mask);
102
103                 break;
104         }
105         case MSG_CONFIG:
106                 print_parse_debug("MSG_CONFIG. size=%d\n", size);
107                 ret = msg_config(&mb);
108                 break;
109         case MSG_SWAP_INST_ADD:
110                 print_parse_debug("MSG_SWAP_INST_ADD. size=%d\n", size);
111                 ret = msg_swap_inst_add(&mb);
112                 break;
113         case MSG_SWAP_INST_REMOVE:
114                 print_parse_debug("MSG_SWAP_INST_REMOVE. size=%d\n", size);
115                 ret = msg_swap_inst_remove(&mb);
116                 break;
117         default:
118                 print_err("incorrect message ID [%u]. size=%d\n", msg_id, size);
119                 ret = -EINVAL;
120                 break;
121         }
122
123 uninit:
124         uninit_mb(&mb);
125         return ret;
126 }
127
128 static void register_msg_handler(void)
129 {
130         set_msg_handler(msg_handler);
131 }
132
133 static void unregister_msg_handler(void)
134 {
135         set_msg_handler(NULL);
136 }
137
138 static int __init swap_parser_init(void)
139 {
140         int ret;
141
142         ret = init_cpu_deps();
143         if (ret)
144                 goto out;
145
146         register_msg_handler();
147
148         ret = init_cmd();
149
150 out:
151         return ret;
152 }
153
154 static void __exit swap_parser_exit(void)
155 {
156         uninit_cmd();
157         unregister_msg_handler();
158 }
159
160 module_init(swap_parser_init);
161 module_exit(swap_parser_exit);
162
163 MODULE_LICENSE("GPL");