[POWERPC] spufs: Call spu_acquire_saved() before calculating the SPU note sizes
[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 __user *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 = copy_to_user(buffer, &data, 8);
45         return ret ? -EFAULT : 8;
46 }
47
48 /*
49  * These are the only things you should do on a core-file: use only these
50  * functions to write out all the necessary info.
51  */
52 static int spufs_dump_write(struct file *file, const void *addr, int nr)
53 {
54         return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
55 }
56
57 static int spufs_dump_seek(struct file *file, loff_t off)
58 {
59         if (file->f_op->llseek) {
60                 if (file->f_op->llseek(file, off, 0) != off)
61                         return 0;
62         } else
63                 file->f_pos = off;
64         return 1;
65 }
66
67 static u64 ctx_ls_size(struct spu_context *ctx)
68 {
69         return ctx->csa.priv2.spu_lslr_RW + 1;
70 }
71
72 static int spufs_ctx_note_size(struct spu_context *ctx, int dfd)
73 {
74         int i, sz, total = 0;
75         char *name;
76         char fullname[80];
77
78         for (i = 0; spufs_coredump_read[i].name; i++) {
79                 name = spufs_coredump_read[i].name;
80                 sz = spufs_coredump_read[i].size;
81
82                 sprintf(fullname, "SPU/%d/%s", dfd, name);
83
84                 total += sizeof(struct elf_note);
85                 total += roundup(strlen(fullname) + 1, 4);
86                 if (!strcmp(name, "mem"))
87                         total += roundup(ctx_ls_size(ctx), 4);
88                 else
89                         total += roundup(sz, 4);
90         }
91
92         return total;
93 }
94
95 /*
96  * The additional architecture-specific notes for Cell are various
97  * context files in the spu context.
98  *
99  * This function iterates over all open file descriptors and sees
100  * if they are a directory in spufs.  In that case we use spufs
101  * internal functionality to dump them without needing to actually
102  * open the files.
103  */
104 static struct spu_context *coredump_next_context(int *fd)
105 {
106         struct fdtable *fdt = files_fdtable(current->files);
107         struct file *file;
108         struct spu_context *ctx = NULL;
109
110         for (; *fd < fdt->max_fds; (*fd)++) {
111                 if (!FD_ISSET(*fd, fdt->open_fds))
112                         continue;
113
114                 file = fcheck(*fd);
115
116                 if (!file || file->f_op != &spufs_context_fops)
117                         continue;
118
119                 ctx = SPUFS_I(file->f_dentry->d_inode)->i_ctx;
120                 if (ctx->flags & SPU_CREATE_NOSCHED)
121                         continue;
122
123                 /* start searching the next fd next time we're called */
124                 (*fd)++;
125                 break;
126         }
127
128         return ctx;
129 }
130
131 static int spufs_arch_notes_size(void)
132 {
133         struct spu_context *ctx;
134         int size = 0, rc, fd;
135
136         fd = 0;
137         while ((ctx = coredump_next_context(&fd)) != NULL) {
138                 spu_acquire_saved(ctx);
139                 rc = spufs_ctx_note_size(ctx, fd);
140                 spu_release_saved(ctx);
141                 if (rc < 0)
142                         break;
143
144                 size += rc;
145         }
146
147         return size;
148 }
149
150 static void spufs_arch_write_note(struct spu_context *ctx, int i,
151                                 struct file *file, int dfd)
152 {
153         loff_t pos = 0;
154         int sz, rc, total = 0;
155         const int bufsz = PAGE_SIZE;
156         char *name;
157         char fullname[80], *buf;
158         struct elf_note en;
159
160         buf = (void *)get_zeroed_page(GFP_KERNEL);
161         if (!buf)
162                 return;
163
164         name = spufs_coredump_read[i].name;
165
166         if (!strcmp(name, "mem"))
167                 sz = ctx_ls_size(ctx);
168         else
169                 sz = spufs_coredump_read[i].size;
170
171         sprintf(fullname, "SPU/%d/%s", dfd, name);
172         en.n_namesz = strlen(fullname) + 1;
173         en.n_descsz = sz;
174         en.n_type = NT_SPU;
175
176         if (!spufs_dump_write(file, &en, sizeof(en)))
177                 goto out;
178         if (!spufs_dump_write(file, fullname, en.n_namesz))
179                 goto out;
180         if (!spufs_dump_seek(file, roundup((unsigned long)file->f_pos, 4)))
181                 goto out;
182
183         do {
184                 rc = do_coredump_read(i, ctx, buf, bufsz, &pos);
185                 if (rc > 0) {
186                         if (!spufs_dump_write(file, buf, rc))
187                                 goto out;
188                         total += rc;
189                 }
190         } while (rc == bufsz && total < sz);
191
192         spufs_dump_seek(file, roundup((unsigned long)file->f_pos
193                                                 - total + sz, 4));
194 out:
195         free_page((unsigned long)buf);
196 }
197
198 static void spufs_arch_write_notes(struct file *file)
199 {
200         struct spu_context *ctx;
201         int fd, j;
202
203         fd = 0;
204         while ((ctx = coredump_next_context(&fd)) != NULL) {
205                 spu_acquire_saved(ctx);
206
207                 for (j = 0; j < spufs_coredump_num_notes; j++)
208                         spufs_arch_write_note(ctx, j, file, fd);
209
210                 spu_release_saved(ctx);
211         }
212 }
213
214 struct spu_coredump_calls spufs_coredump_calls = {
215         .arch_notes_size = spufs_arch_notes_size,
216         .arch_write_notes = spufs_arch_write_notes,
217         .owner = THIS_MODULE,
218 };