c65b7178e77b741f65be0ecb5603c5db51050842
[safe/jmp/linux-2.6] / arch / powerpc / platforms / cell / spufs / coredump.c
1 /*
2  * SPU core dump code
3  *
4  * (C) Copyright 2006 IBM Corp.
5  *
6  * Author: Dwayne Grant McConnell <decimal@us.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/elf.h>
24 #include <linux/file.h>
25 #include <linux/fs.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/syscalls.h>
29
30 #include <asm/uaccess.h>
31
32 #include "spufs.h"
33
34 static ssize_t do_coredump_read(int num, struct spu_context *ctx, void *buffer,
35                                 size_t size, loff_t *off)
36 {
37         u64 data;
38         int ret;
39
40         if (spufs_coredump_read[num].read)
41                 return spufs_coredump_read[num].read(ctx, buffer, size, off);
42
43         data = spufs_coredump_read[num].get(ctx);
44         ret = snprintf(buffer, size, "0x%.16lx", data);
45         if (ret >= size)
46                 return size;
47         return ++ret; /* count trailing NULL */
48 }
49
50 /*
51  * These are the only things you should do on a core-file: use only these
52  * functions to write out all the necessary info.
53  */
54 static int spufs_dump_write(struct file *file, const void *addr, int nr)
55 {
56         return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
57 }
58
59 static int spufs_dump_seek(struct file *file, loff_t off)
60 {
61         if (file->f_op->llseek) {
62                 if (file->f_op->llseek(file, off, 0) != off)
63                         return 0;
64         } else
65                 file->f_pos = off;
66         return 1;
67 }
68
69 static u64 ctx_ls_size(struct spu_context *ctx)
70 {
71         return ctx->csa.priv2.spu_lslr_RW + 1;
72 }
73
74 static int spufs_ctx_note_size(struct spu_context *ctx, int dfd)
75 {
76         int i, sz, total = 0;
77         char *name;
78         char fullname[80];
79
80         for (i = 0; spufs_coredump_read[i].name; i++) {
81                 name = spufs_coredump_read[i].name;
82                 sz = spufs_coredump_read[i].size;
83
84                 sprintf(fullname, "SPU/%d/%s", dfd, name);
85
86                 total += sizeof(struct elf_note);
87                 total += roundup(strlen(fullname) + 1, 4);
88                 if (!strcmp(name, "mem"))
89                         total += roundup(ctx_ls_size(ctx), 4);
90                 else
91                         total += roundup(sz, 4);
92         }
93
94         return total;
95 }
96
97 /*
98  * The additional architecture-specific notes for Cell are various
99  * context files in the spu context.
100  *
101  * This function iterates over all open file descriptors and sees
102  * if they are a directory in spufs.  In that case we use spufs
103  * internal functionality to dump them without needing to actually
104  * open the files.
105  */
106 static struct spu_context *coredump_next_context(int *fd)
107 {
108         struct fdtable *fdt = files_fdtable(current->files);
109         struct file *file;
110         struct spu_context *ctx = NULL;
111
112         for (; *fd < fdt->max_fds; (*fd)++) {
113                 if (!FD_ISSET(*fd, fdt->open_fds))
114                         continue;
115
116                 file = fcheck(*fd);
117
118                 if (!file || file->f_op != &spufs_context_fops)
119                         continue;
120
121                 ctx = SPUFS_I(file->f_dentry->d_inode)->i_ctx;
122                 if (ctx->flags & SPU_CREATE_NOSCHED)
123                         continue;
124
125                 /* start searching the next fd next time we're called */
126                 (*fd)++;
127                 break;
128         }
129
130         return ctx;
131 }
132
133 static int spufs_arch_notes_size(void)
134 {
135         struct spu_context *ctx;
136         int size = 0, rc, fd;
137
138         fd = 0;
139         while ((ctx = coredump_next_context(&fd)) != NULL) {
140                 spu_acquire_saved(ctx);
141                 rc = spufs_ctx_note_size(ctx, fd);
142                 spu_release_saved(ctx);
143                 if (rc < 0)
144                         break;
145
146                 size += rc;
147         }
148
149         return size;
150 }
151
152 static void spufs_arch_write_note(struct spu_context *ctx, int i,
153                                 struct file *file, int dfd)
154 {
155         loff_t pos = 0;
156         int sz, rc, total = 0;
157         const int bufsz = PAGE_SIZE;
158         char *name;
159         char fullname[80], *buf;
160         struct elf_note en;
161
162         buf = (void *)get_zeroed_page(GFP_KERNEL);
163         if (!buf)
164                 return;
165
166         name = spufs_coredump_read[i].name;
167
168         if (!strcmp(name, "mem"))
169                 sz = ctx_ls_size(ctx);
170         else
171                 sz = spufs_coredump_read[i].size;
172
173         sprintf(fullname, "SPU/%d/%s", dfd, name);
174         en.n_namesz = strlen(fullname) + 1;
175         en.n_descsz = sz;
176         en.n_type = NT_SPU;
177
178         if (!spufs_dump_write(file, &en, sizeof(en)))
179                 goto out;
180         if (!spufs_dump_write(file, fullname, en.n_namesz))
181                 goto out;
182         if (!spufs_dump_seek(file, roundup((unsigned long)file->f_pos, 4)))
183                 goto out;
184
185         do {
186                 rc = do_coredump_read(i, ctx, buf, bufsz, &pos);
187                 if (rc > 0) {
188                         if (!spufs_dump_write(file, buf, rc))
189                                 goto out;
190                         total += rc;
191                 }
192         } while (rc == bufsz && total < sz);
193
194         spufs_dump_seek(file, roundup((unsigned long)file->f_pos
195                                                 - total + sz, 4));
196 out:
197         free_page((unsigned long)buf);
198 }
199
200 static void spufs_arch_write_notes(struct file *file)
201 {
202         struct spu_context *ctx;
203         int fd, j;
204
205         fd = 0;
206         while ((ctx = coredump_next_context(&fd)) != NULL) {
207                 spu_acquire_saved(ctx);
208
209                 for (j = 0; j < spufs_coredump_num_notes; j++)
210                         spufs_arch_write_note(ctx, j, file, fd);
211
212                 spu_release_saved(ctx);
213         }
214 }
215
216 struct spu_coredump_calls spufs_coredump_calls = {
217         .arch_notes_size = spufs_arch_notes_size,
218         .arch_write_notes = spufs_arch_write_notes,
219         .owner = THIS_MODULE,
220 };