blob: 7f09a56818009b8d56c421c298ce3ef6d8dd6c9c [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070014#include <linux/workqueue.h>
15#include <linux/io.h>
16#include <linux/jiffies.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070017#include <linux/sched.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070018#include <linux/module.h>
19#include <linux/miscdevice.h>
20#include <linux/fs.h>
21#include <linux/mm.h>
22#include <linux/slab.h>
23#include <linux/poll.h>
24#include <linux/uaccess.h>
Stephen Boyd73bab7f2012-11-29 12:41:29 -080025#include <linux/elf.h>
Stephen Boyd903c02b2012-12-06 11:43:31 -080026#include <linux/wait.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027
28#include "ramdump.h"
29
30#define RAMDUMP_WAIT_MSECS 120000
31
32struct ramdump_device {
33 char name[256];
34
35 unsigned int data_ready;
36 unsigned int consumer_present;
37 int ramdump_status;
38
39 struct completion ramdump_complete;
40 struct miscdevice device;
41
42 wait_queue_head_t dump_wait_q;
43 int nsegments;
44 struct ramdump_segment *segments;
Stephen Boyd73bab7f2012-11-29 12:41:29 -080045 size_t elfcore_size;
46 char *elfcore_buf;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047};
48
49static int ramdump_open(struct inode *inode, struct file *filep)
50{
51 struct ramdump_device *rd_dev = container_of(filep->private_data,
52 struct ramdump_device, device);
53 rd_dev->consumer_present = 1;
54 rd_dev->ramdump_status = 0;
55 return 0;
56}
57
58static int ramdump_release(struct inode *inode, struct file *filep)
59{
60 struct ramdump_device *rd_dev = container_of(filep->private_data,
61 struct ramdump_device, device);
62 rd_dev->consumer_present = 0;
63 rd_dev->data_ready = 0;
64 complete(&rd_dev->ramdump_complete);
65 return 0;
66}
67
68static unsigned long offset_translate(loff_t user_offset,
69 struct ramdump_device *rd_dev, unsigned long *data_left)
70{
71 int i = 0;
72
73 for (i = 0; i < rd_dev->nsegments; i++)
74 if (user_offset >= rd_dev->segments[i].size)
75 user_offset -= rd_dev->segments[i].size;
76 else
77 break;
78
79 if (i == rd_dev->nsegments) {
80 pr_debug("Ramdump(%s): offset_translate returning zero\n",
81 rd_dev->name);
82 *data_left = 0;
83 return 0;
84 }
85
86 *data_left = rd_dev->segments[i].size - user_offset;
87
88 pr_debug("Ramdump(%s): Returning address: %llx, data_left = %ld\n",
89 rd_dev->name, rd_dev->segments[i].address + user_offset,
90 *data_left);
91
92 return rd_dev->segments[i].address + user_offset;
93}
94
95#define MAX_IOREMAP_SIZE SZ_1M
96
97static int ramdump_read(struct file *filep, char __user *buf, size_t count,
98 loff_t *pos)
99{
100 struct ramdump_device *rd_dev = container_of(filep->private_data,
101 struct ramdump_device, device);
102 void *device_mem = NULL;
103 unsigned long data_left = 0;
104 unsigned long addr = 0;
105 size_t copy_size = 0;
106 int ret = 0;
Stephen Boyd5500a8e2013-02-05 15:21:43 -0800107 loff_t orig_pos = *pos;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108
Stephen Boyde8c274f2012-11-29 15:58:57 -0800109 if ((filep->f_flags & O_NONBLOCK) && !rd_dev->data_ready)
110 return -EAGAIN;
111
112 ret = wait_event_interruptible(rd_dev->dump_wait_q, rd_dev->data_ready);
113 if (ret)
114 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115
Stephen Boyd73bab7f2012-11-29 12:41:29 -0800116 if (*pos < rd_dev->elfcore_size) {
Stephen Boyd5500a8e2013-02-05 15:21:43 -0800117 copy_size = rd_dev->elfcore_size - *pos;
118 copy_size = min(copy_size, count);
Stephen Boyd73bab7f2012-11-29 12:41:29 -0800119
Stephen Boyd5500a8e2013-02-05 15:21:43 -0800120 if (copy_to_user(buf, rd_dev->elfcore_buf + *pos, copy_size)) {
Stephen Boyd73bab7f2012-11-29 12:41:29 -0800121 ret = -EFAULT;
122 goto ramdump_done;
123 }
124 *pos += copy_size;
125 count -= copy_size;
126 buf += copy_size;
127 if (count == 0)
128 return copy_size;
129 }
130
131 addr = offset_translate(*pos - rd_dev->elfcore_size, rd_dev,
132 &data_left);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133
134 /* EOF check */
135 if (data_left == 0) {
136 pr_debug("Ramdump(%s): Ramdump complete. %lld bytes read.",
137 rd_dev->name, *pos);
138 rd_dev->ramdump_status = 0;
139 ret = 0;
140 goto ramdump_done;
141 }
142
143 copy_size = min(count, (size_t)MAX_IOREMAP_SIZE);
144 copy_size = min((unsigned long)copy_size, data_left);
145 device_mem = ioremap_nocache(addr, copy_size);
146
147 if (device_mem == NULL) {
148 pr_err("Ramdump(%s): Unable to ioremap: addr %lx, size %x\n",
149 rd_dev->name, addr, copy_size);
150 rd_dev->ramdump_status = -1;
151 ret = -ENOMEM;
152 goto ramdump_done;
153 }
154
155 if (copy_to_user(buf, device_mem, copy_size)) {
156 pr_err("Ramdump(%s): Couldn't copy all data to user.",
157 rd_dev->name);
158 iounmap(device_mem);
159 rd_dev->ramdump_status = -1;
160 ret = -EFAULT;
161 goto ramdump_done;
162 }
163
164 iounmap(device_mem);
165 *pos += copy_size;
166
167 pr_debug("Ramdump(%s): Read %d bytes from address %lx.",
168 rd_dev->name, copy_size, addr);
169
Stephen Boyd5500a8e2013-02-05 15:21:43 -0800170 return *pos - orig_pos;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171
172ramdump_done:
173 rd_dev->data_ready = 0;
174 *pos = 0;
175 complete(&rd_dev->ramdump_complete);
176 return ret;
177}
178
179static unsigned int ramdump_poll(struct file *filep,
180 struct poll_table_struct *wait)
181{
182 struct ramdump_device *rd_dev = container_of(filep->private_data,
183 struct ramdump_device, device);
184 unsigned int mask = 0;
185
186 if (rd_dev->data_ready)
187 mask |= (POLLIN | POLLRDNORM);
188
189 poll_wait(filep, &rd_dev->dump_wait_q, wait);
190 return mask;
191}
192
Stephen Boyd903c02b2012-12-06 11:43:31 -0800193static const struct file_operations ramdump_file_ops = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194 .open = ramdump_open,
195 .release = ramdump_release,
196 .read = ramdump_read,
197 .poll = ramdump_poll
198};
199
Stephen Boydc1a72612012-07-05 14:07:35 -0700200void *create_ramdump_device(const char *dev_name, struct device *parent)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201{
202 int ret;
203 struct ramdump_device *rd_dev;
204
205 if (!dev_name) {
206 pr_err("%s: Invalid device name.\n", __func__);
207 return NULL;
208 }
209
210 rd_dev = kzalloc(sizeof(struct ramdump_device), GFP_KERNEL);
211
212 if (!rd_dev) {
213 pr_err("%s: Couldn't alloc space for ramdump device!",
214 __func__);
215 return NULL;
216 }
217
Matt Wagantallf9563142011-11-10 19:04:51 -0800218 snprintf(rd_dev->name, ARRAY_SIZE(rd_dev->name), "ramdump_%s",
219 dev_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220
221 init_completion(&rd_dev->ramdump_complete);
222
223 rd_dev->device.minor = MISC_DYNAMIC_MINOR;
224 rd_dev->device.name = rd_dev->name;
225 rd_dev->device.fops = &ramdump_file_ops;
Stephen Boydc1a72612012-07-05 14:07:35 -0700226 rd_dev->device.parent = parent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227
228 init_waitqueue_head(&rd_dev->dump_wait_q);
229
230 ret = misc_register(&rd_dev->device);
231
232 if (ret) {
233 pr_err("%s: misc_register failed for %s (%d)", __func__,
234 dev_name, ret);
235 kfree(rd_dev);
236 return NULL;
237 }
238
239 return (void *)rd_dev;
240}
241
Stephen Boydb9fab142012-06-20 15:33:32 -0700242void destroy_ramdump_device(void *dev)
243{
244 struct ramdump_device *rd_dev = dev;
245
246 if (IS_ERR_OR_NULL(rd_dev))
247 return;
248
249 misc_deregister(&rd_dev->device);
250 kfree(rd_dev);
251}
252
Stephen Boyd73bab7f2012-11-29 12:41:29 -0800253static int _do_ramdump(void *handle, struct ramdump_segment *segments,
254 int nsegments, bool use_elf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255{
256 int ret, i;
257 struct ramdump_device *rd_dev = (struct ramdump_device *)handle;
Stephen Boyd73bab7f2012-11-29 12:41:29 -0800258 Elf32_Phdr *phdr;
259 Elf32_Ehdr *ehdr;
260 unsigned long offset;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261
262 if (!rd_dev->consumer_present) {
263 pr_err("Ramdump(%s): No consumers. Aborting..\n", rd_dev->name);
264 return -EPIPE;
265 }
266
267 for (i = 0; i < nsegments; i++)
268 segments[i].size = PAGE_ALIGN(segments[i].size);
269
270 rd_dev->segments = segments;
271 rd_dev->nsegments = nsegments;
272
Stephen Boyd73bab7f2012-11-29 12:41:29 -0800273 if (use_elf) {
274 rd_dev->elfcore_size = sizeof(*ehdr) +
275 sizeof(*phdr) * nsegments;
276 ehdr = kzalloc(rd_dev->elfcore_size, GFP_KERNEL);
277 rd_dev->elfcore_buf = (char *)ehdr;
278 if (!rd_dev->elfcore_buf)
279 return -ENOMEM;
280
281 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
282 ehdr->e_ident[EI_CLASS] = ELFCLASS32;
283 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
284 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
285 ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
286 ehdr->e_type = ET_CORE;
287 ehdr->e_version = EV_CURRENT;
288 ehdr->e_phoff = sizeof(*ehdr);
289 ehdr->e_ehsize = sizeof(*ehdr);
290 ehdr->e_phentsize = sizeof(*phdr);
291 ehdr->e_phnum = nsegments;
292
293 offset = rd_dev->elfcore_size;
294 phdr = (Elf32_Phdr *)(ehdr + 1);
295 for (i = 0; i < nsegments; i++, phdr++) {
296 phdr->p_type = PT_LOAD;
297 phdr->p_offset = offset;
298 phdr->p_vaddr = phdr->p_paddr = segments[i].address;
299 phdr->p_filesz = phdr->p_memsz = segments[i].size;
300 phdr->p_flags = PF_R | PF_W | PF_X;
301 offset += phdr->p_filesz;
302 }
303 }
304
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 rd_dev->data_ready = 1;
306 rd_dev->ramdump_status = -1;
307
308 INIT_COMPLETION(rd_dev->ramdump_complete);
309
310 /* Tell userspace that the data is ready */
311 wake_up(&rd_dev->dump_wait_q);
312
313 /* Wait (with a timeout) to let the ramdump complete */
314 ret = wait_for_completion_timeout(&rd_dev->ramdump_complete,
315 msecs_to_jiffies(RAMDUMP_WAIT_MSECS));
316
317 if (!ret) {
318 pr_err("Ramdump(%s): Timed out waiting for userspace.\n",
319 rd_dev->name);
320 ret = -EPIPE;
321 } else
322 ret = (rd_dev->ramdump_status == 0) ? 0 : -EPIPE;
323
324 rd_dev->data_ready = 0;
Stephen Boyd73bab7f2012-11-29 12:41:29 -0800325 rd_dev->elfcore_size = 0;
326 kfree(rd_dev->elfcore_buf);
327 rd_dev->elfcore_buf = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 return ret;
Stephen Boyd73bab7f2012-11-29 12:41:29 -0800329
330}
331
332int do_ramdump(void *handle, struct ramdump_segment *segments, int nsegments)
333{
334 return _do_ramdump(handle, segments, nsegments, false);
335}
336
337int
338do_elf_ramdump(void *handle, struct ramdump_segment *segments, int nsegments)
339{
340 return _do_ramdump(handle, segments, nsegments, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341}