]> err.no Git - linux-2.6/blob - arch/powerpc/platforms/cell/spufs/coredump.c
[POWERPC] spufs: Handle errors in SPU coredump code, and support coredump to a pipe
[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, loff_t *foffset)
55 {
56         ssize_t written;
57
58         written = file->f_op->write(file, addr, nr, &file->f_pos);
59         *foffset += written;
60
61         if (written != nr)
62                 return -EIO;
63
64         return 0;
65 }
66
67 static int spufs_dump_align(struct file *file, char *buf, loff_t new_off,
68                             loff_t *foffset)
69 {
70         int rc, size;
71
72         size = min((loff_t)PAGE_SIZE, new_off - *foffset);
73         memset(buf, 0, size);
74
75         rc = 0;
76         while (rc == 0 && new_off > *foffset) {
77                 size = min((loff_t)PAGE_SIZE, new_off - *foffset);
78                 rc = spufs_dump_write(file, buf, size, foffset);
79         }
80
81         return rc;
82 }
83
84 static int spufs_ctx_note_size(struct spu_context *ctx, int dfd)
85 {
86         int i, sz, total = 0;
87         char *name;
88         char fullname[80];
89
90         for (i = 0; spufs_coredump_read[i].name != NULL; i++) {
91                 name = spufs_coredump_read[i].name;
92                 sz = spufs_coredump_read[i].size;
93
94                 sprintf(fullname, "SPU/%d/%s", dfd, name);
95
96                 total += sizeof(struct elf_note);
97                 total += roundup(strlen(fullname) + 1, 4);
98                 total += roundup(sz, 4);
99         }
100
101         return total;
102 }
103
104 /*
105  * The additional architecture-specific notes for Cell are various
106  * context files in the spu context.
107  *
108  * This function iterates over all open file descriptors and sees
109  * if they are a directory in spufs.  In that case we use spufs
110  * internal functionality to dump them without needing to actually
111  * open the files.
112  */
113 static struct spu_context *coredump_next_context(int *fd)
114 {
115         struct fdtable *fdt = files_fdtable(current->files);
116         struct file *file;
117         struct spu_context *ctx = NULL;
118
119         for (; *fd < fdt->max_fds; (*fd)++) {
120                 if (!FD_ISSET(*fd, fdt->open_fds))
121                         continue;
122
123                 file = fcheck(*fd);
124
125                 if (!file || file->f_op != &spufs_context_fops)
126                         continue;
127
128                 ctx = SPUFS_I(file->f_dentry->d_inode)->i_ctx;
129                 if (ctx->flags & SPU_CREATE_NOSCHED)
130                         continue;
131
132                 /* start searching the next fd next time we're called */
133                 (*fd)++;
134                 break;
135         }
136
137         return ctx;
138 }
139
140 int spufs_coredump_extra_notes_size(void)
141 {
142         struct spu_context *ctx;
143         int size = 0, rc, fd;
144
145         fd = 0;
146         while ((ctx = coredump_next_context(&fd)) != NULL) {
147                 spu_acquire_saved(ctx);
148                 rc = spufs_ctx_note_size(ctx, fd);
149                 spu_release_saved(ctx);
150                 if (rc < 0)
151                         break;
152
153                 size += rc;
154         }
155
156         return size;
157 }
158
159 static int spufs_arch_write_note(struct spu_context *ctx, int i,
160                                   struct file *file, int dfd, loff_t *foffset)
161 {
162         loff_t pos = 0;
163         int sz, rc, nread, total = 0;
164         const int bufsz = PAGE_SIZE;
165         char *name;
166         char fullname[80], *buf;
167         struct elf_note en;
168
169         buf = (void *)get_zeroed_page(GFP_KERNEL);
170         if (!buf)
171                 return -ENOMEM;
172
173         name = spufs_coredump_read[i].name;
174         sz = spufs_coredump_read[i].size;
175
176         sprintf(fullname, "SPU/%d/%s", dfd, name);
177         en.n_namesz = strlen(fullname) + 1;
178         en.n_descsz = sz;
179         en.n_type = NT_SPU;
180
181         rc = spufs_dump_write(file, &en, sizeof(en), foffset);
182         if (rc)
183                 goto out;
184
185         rc = spufs_dump_write(file, fullname, en.n_namesz, foffset);
186         if (rc)
187                 goto out;
188
189         rc = spufs_dump_align(file, buf, roundup(*foffset, 4), foffset);
190         if (rc)
191                 goto out;
192
193         do {
194                 nread = do_coredump_read(i, ctx, buf, bufsz, &pos);
195                 if (nread > 0) {
196                         rc = spufs_dump_write(file, buf, nread, foffset);
197                         if (rc)
198                                 goto out;
199                         total += nread;
200                 }
201         } while (nread == bufsz && total < sz);
202
203         if (nread < 0) {
204                 rc = nread;
205                 goto out;
206         }
207
208         rc = spufs_dump_align(file, buf, roundup(*foffset - total + sz, 4),
209                               foffset);
210
211 out:
212         free_page((unsigned long)buf);
213         return rc;
214 }
215
216 int spufs_coredump_extra_notes_write(struct file *file, loff_t *foffset)
217 {
218         struct spu_context *ctx;
219         int fd, j, rc;
220
221         fd = 0;
222         while ((ctx = coredump_next_context(&fd)) != NULL) {
223                 spu_acquire_saved(ctx);
224
225                 for (j = 0; spufs_coredump_read[j].name != NULL; j++) {
226                         rc = spufs_arch_write_note(ctx, j, file, fd, foffset);
227                         if (rc) {
228                                 spu_release_saved(ctx);
229                                 return rc;
230                         }
231                 }
232
233                 spu_release_saved(ctx);
234         }
235
236         return 0;
237 }