blob: be6212ddbf06a8a097ac19d361b2f420da35ef6a [file] [log] [blame]
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001/*
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>
Al Viro9f3acc32008-04-24 07:44:08 -040025#include <linux/fdtable.h>
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010026#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/gfp.h>
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010028#include <linux/list.h>
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010029#include <linux/syscalls.h>
Al Virocdc3d562013-10-05 22:24:29 -040030#include <linux/coredump.h>
31#include <linux/binfmts.h>
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010032
33#include <asm/uaccess.h>
34
35#include "spufs.h"
36
Michael Ellermand464fb42007-09-19 14:38:12 +100037static ssize_t do_coredump_read(int num, struct spu_context *ctx, void *buffer,
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010038 size_t size, loff_t *off)
39{
40 u64 data;
41 int ret;
42
43 if (spufs_coredump_read[num].read)
44 return spufs_coredump_read[num].read(ctx, buffer, size, off);
45
46 data = spufs_coredump_read[num].get(ctx);
Stephen Rothwell9477e452009-01-06 14:27:38 +000047 ret = snprintf(buffer, size, "0x%.16llx", data);
Michael Ellermand464fb42007-09-19 14:38:12 +100048 if (ret >= size)
49 return size;
50 return ++ret; /* count trailing NULL */
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010051}
52
Michael Ellermanf9b7bbe2007-09-19 14:38:12 +100053static int spufs_ctx_note_size(struct spu_context *ctx, int dfd)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010054{
Michael Ellermanf9b7bbe2007-09-19 14:38:12 +100055 int i, sz, total = 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010056 char *name;
57 char fullname[80];
58
Michael Ellerman936d5bf2007-09-19 14:38:12 +100059 for (i = 0; spufs_coredump_read[i].name != NULL; i++) {
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010060 name = spufs_coredump_read[i].name;
61 sz = spufs_coredump_read[i].size;
62
63 sprintf(fullname, "SPU/%d/%s", dfd, name);
64
65 total += sizeof(struct elf_note);
66 total += roundup(strlen(fullname) + 1, 4);
Michael Ellerman59000b52007-09-19 14:38:12 +100067 total += roundup(sz, 4);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010068 }
69
70 return total;
71}
72
Al Viro2be7fd52012-08-21 22:50:49 -040073static int match_context(const void *v, struct file *file, unsigned fd)
74{
75 struct spu_context *ctx;
76 if (file->f_op != &spufs_context_fops)
77 return 0;
Al Viro496ad9a2013-01-23 17:07:38 -050078 ctx = SPUFS_I(file_inode(file))->i_ctx;
Al Viro2be7fd52012-08-21 22:50:49 -040079 if (ctx->flags & SPU_CREATE_NOSCHED)
80 return 0;
81 return fd + 1;
82}
83
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010084/*
85 * The additional architecture-specific notes for Cell are various
86 * context files in the spu context.
87 *
88 * This function iterates over all open file descriptors and sees
89 * if they are a directory in spufs. In that case we use spufs
90 * internal functionality to dump them without needing to actually
91 * open the files.
92 */
Al Viro2be7fd52012-08-21 22:50:49 -040093/*
94 * descriptor table is not shared, so files can't change or go away.
95 */
Michael Ellermana595ed62007-09-19 14:38:12 +100096static struct spu_context *coredump_next_context(int *fd)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010097{
Michael Ellermana595ed62007-09-19 14:38:12 +100098 struct file *file;
Al Viro2be7fd52012-08-21 22:50:49 -040099 int n = iterate_fd(current->files, *fd, match_context, NULL);
100 if (!n)
101 return NULL;
102 *fd = n - 1;
103 file = fcheck(*fd);
Al Viro496ad9a2013-01-23 17:07:38 -0500104 return SPUFS_I(file_inode(file))->i_ctx;
Michael Ellermana595ed62007-09-19 14:38:12 +1000105}
106
Michael Ellerman48cad412007-09-19 14:38:12 +1000107int spufs_coredump_extra_notes_size(void)
Michael Ellermana595ed62007-09-19 14:38:12 +1000108{
109 struct spu_context *ctx;
110 int size = 0, rc, fd;
111
112 fd = 0;
113 while ((ctx = coredump_next_context(&fd)) != NULL) {
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900114 rc = spu_acquire_saved(ctx);
115 if (rc)
116 break;
Michael Ellermanf9b7bbe2007-09-19 14:38:12 +1000117 rc = spufs_ctx_note_size(ctx, fd);
Michael Ellerman9a5080f2007-09-19 14:38:12 +1000118 spu_release_saved(ctx);
Michael Ellermana595ed62007-09-19 14:38:12 +1000119 if (rc < 0)
120 break;
121
122 size += rc;
Gerhard Stenzelada397e2008-03-28 14:18:30 +1100123
124 /* start searching the next fd next time */
125 fd++;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100126 }
127
128 return size;
129}
130
Michael Ellerman7af14432007-09-19 14:38:12 +1000131static int spufs_arch_write_note(struct spu_context *ctx, int i,
Al Virocdc3d562013-10-05 22:24:29 -0400132 struct coredump_params *cprm, int dfd)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100133{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100134 loff_t pos = 0;
Al Viro7b1f4022013-10-08 09:44:29 -0400135 int sz, rc, total = 0;
Arnd Bergmann6cf21792007-04-23 21:08:25 +0200136 const int bufsz = PAGE_SIZE;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100137 char *name;
138 char fullname[80], *buf;
139 struct elf_note en;
140
Arnd Bergmann6cf21792007-04-23 21:08:25 +0200141 buf = (void *)get_zeroed_page(GFP_KERNEL);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100142 if (!buf)
Michael Ellerman7af14432007-09-19 14:38:12 +1000143 return -ENOMEM;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100144
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100145 name = spufs_coredump_read[i].name;
Michael Ellerman59000b52007-09-19 14:38:12 +1000146 sz = spufs_coredump_read[i].size;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100147
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100148 sprintf(fullname, "SPU/%d/%s", dfd, name);
149 en.n_namesz = strlen(fullname) + 1;
150 en.n_descsz = sz;
151 en.n_type = NT_SPU;
152
Al Viro7b1f4022013-10-08 09:44:29 -0400153 if (!dump_emit(cprm, &en, sizeof(en)))
154 goto Eio;
Michael Ellerman7af14432007-09-19 14:38:12 +1000155
Al Viro7b1f4022013-10-08 09:44:29 -0400156 if (!dump_emit(cprm, fullname, en.n_namesz))
157 goto Eio;
Michael Ellerman7af14432007-09-19 14:38:12 +1000158
Al Viro22a8cb82013-10-08 11:05:01 -0400159 if (!dump_align(cprm, 4))
Al Viro7b1f4022013-10-08 09:44:29 -0400160 goto Eio;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100161
162 do {
Al Viro7b1f4022013-10-08 09:44:29 -0400163 rc = do_coredump_read(i, ctx, buf, bufsz, &pos);
164 if (rc > 0) {
165 if (!dump_emit(cprm, buf, rc))
166 goto Eio;
167 total += rc;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100168 }
Al Viro7b1f4022013-10-08 09:44:29 -0400169 } while (rc == bufsz && total < sz);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100170
Al Viro7b1f4022013-10-08 09:44:29 -0400171 if (rc < 0)
Michael Ellerman7af14432007-09-19 14:38:12 +1000172 goto out;
Michael Ellerman7af14432007-09-19 14:38:12 +1000173
Al Viro7b1f4022013-10-08 09:44:29 -0400174 if (!dump_skip(cprm,
175 roundup(cprm->written - total + sz, 4) - cprm->written))
176 goto Eio;
Arnd Bergmann6cf21792007-04-23 21:08:25 +0200177out:
178 free_page((unsigned long)buf);
Michael Ellerman7af14432007-09-19 14:38:12 +1000179 return rc;
Al Viro7b1f4022013-10-08 09:44:29 -0400180Eio:
181 free_page((unsigned long)buf);
182 return -EIO;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100183}
184
Al Virocdc3d562013-10-05 22:24:29 -0400185int spufs_coredump_extra_notes_write(struct coredump_params *cprm)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100186{
Michael Ellermanf9b7bbe2007-09-19 14:38:12 +1000187 struct spu_context *ctx;
Michael Ellerman7af14432007-09-19 14:38:12 +1000188 int fd, j, rc;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100189
Michael Ellermanf9b7bbe2007-09-19 14:38:12 +1000190 fd = 0;
191 while ((ctx = coredump_next_context(&fd)) != NULL) {
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900192 rc = spu_acquire_saved(ctx);
193 if (rc)
194 return rc;
Michael Ellermanf9b7bbe2007-09-19 14:38:12 +1000195
Michael Ellerman7af14432007-09-19 14:38:12 +1000196 for (j = 0; spufs_coredump_read[j].name != NULL; j++) {
Al Virocdc3d562013-10-05 22:24:29 -0400197 rc = spufs_arch_write_note(ctx, j, cprm, fd);
Michael Ellerman7af14432007-09-19 14:38:12 +1000198 if (rc) {
199 spu_release_saved(ctx);
200 return rc;
201 }
202 }
Michael Ellermanf9b7bbe2007-09-19 14:38:12 +1000203
204 spu_release_saved(ctx);
Gerhard Stenzelada397e2008-03-28 14:18:30 +1100205
206 /* start searching the next fd next time */
207 fd++;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100208 }
Michael Ellerman7af14432007-09-19 14:38:12 +1000209
210 return 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100211}