4 * (C) Copyright 2006 IBM Corp.
6 * Author: Dwayne Grant McConnell <decimal@us.ibm.com>
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)
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.
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.
23 #include <linux/elf.h>
24 #include <linux/file.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/syscalls.h>
30 #include <asm/uaccess.h>
34 static ssize_t do_coredump_read(int num, struct spu_context *ctx, void *buffer,
35 size_t size, loff_t *off)
40 if (spufs_coredump_read[num].read)
41 return spufs_coredump_read[num].read(ctx, buffer, size, off);
43 data = spufs_coredump_read[num].get(ctx);
44 ret = snprintf(buffer, size, "0x%.16lx", data);
47 return ++ret; /* count trailing NULL */
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.
54 static int spufs_dump_write(struct file *file, const void *addr, int nr)
56 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
59 static int spufs_dump_seek(struct file *file, loff_t off)
61 if (file->f_op->llseek) {
62 if (file->f_op->llseek(file, off, 0) != off)
69 static int spufs_ctx_note_size(struct spu_context *ctx, int dfd)
75 for (i = 0; spufs_coredump_read[i].name; i++) {
76 name = spufs_coredump_read[i].name;
77 sz = spufs_coredump_read[i].size;
79 sprintf(fullname, "SPU/%d/%s", dfd, name);
81 total += sizeof(struct elf_note);
82 total += roundup(strlen(fullname) + 1, 4);
83 total += roundup(sz, 4);
90 * The additional architecture-specific notes for Cell are various
91 * context files in the spu context.
93 * This function iterates over all open file descriptors and sees
94 * if they are a directory in spufs. In that case we use spufs
95 * internal functionality to dump them without needing to actually
98 static struct spu_context *coredump_next_context(int *fd)
100 struct fdtable *fdt = files_fdtable(current->files);
102 struct spu_context *ctx = NULL;
104 for (; *fd < fdt->max_fds; (*fd)++) {
105 if (!FD_ISSET(*fd, fdt->open_fds))
110 if (!file || file->f_op != &spufs_context_fops)
113 ctx = SPUFS_I(file->f_dentry->d_inode)->i_ctx;
114 if (ctx->flags & SPU_CREATE_NOSCHED)
117 /* start searching the next fd next time we're called */
125 static int spufs_arch_notes_size(void)
127 struct spu_context *ctx;
128 int size = 0, rc, fd;
131 while ((ctx = coredump_next_context(&fd)) != NULL) {
132 spu_acquire_saved(ctx);
133 rc = spufs_ctx_note_size(ctx, fd);
134 spu_release_saved(ctx);
144 static void spufs_arch_write_note(struct spu_context *ctx, int i,
145 struct file *file, int dfd)
148 int sz, rc, total = 0;
149 const int bufsz = PAGE_SIZE;
151 char fullname[80], *buf;
154 buf = (void *)get_zeroed_page(GFP_KERNEL);
158 name = spufs_coredump_read[i].name;
159 sz = spufs_coredump_read[i].size;
161 sprintf(fullname, "SPU/%d/%s", dfd, name);
162 en.n_namesz = strlen(fullname) + 1;
166 if (!spufs_dump_write(file, &en, sizeof(en)))
168 if (!spufs_dump_write(file, fullname, en.n_namesz))
170 if (!spufs_dump_seek(file, roundup((unsigned long)file->f_pos, 4)))
174 rc = do_coredump_read(i, ctx, buf, bufsz, &pos);
176 if (!spufs_dump_write(file, buf, rc))
180 } while (rc == bufsz && total < sz);
182 spufs_dump_seek(file, roundup((unsigned long)file->f_pos
185 free_page((unsigned long)buf);
188 static void spufs_arch_write_notes(struct file *file)
190 struct spu_context *ctx;
194 while ((ctx = coredump_next_context(&fd)) != NULL) {
195 spu_acquire_saved(ctx);
197 for (j = 0; j < spufs_coredump_num_notes; j++)
198 spufs_arch_write_note(ctx, j, file, fd);
200 spu_release_saved(ctx);
204 struct spu_coredump_calls spufs_coredump_calls = {
205 .arch_notes_size = spufs_arch_notes_size,
206 .arch_write_notes = spufs_arch_write_notes,
207 .owner = THIS_MODULE,