1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * SPU core dump code
4  *
5  * (C) Copyright 2006 IBM Corp.
6  *
7  * Author: Dwayne Grant McConnell <decimal@us.ibm.com>
8  */
9 
10 #include <linux/elf.h>
11 #include <linux/file.h>
12 #include <linux/fdtable.h>
13 #include <linux/fs.h>
14 #include <linux/gfp.h>
15 #include <linux/list.h>
16 #include <linux/syscalls.h>
17 #include <linux/coredump.h>
18 #include <linux/binfmts.h>
19 
20 #include <linux/uaccess.h>
21 
22 #include "spufs.h"
23 
spufs_ctx_note_size(struct spu_context * ctx,int dfd)24 static int spufs_ctx_note_size(struct spu_context *ctx, int dfd)
25 {
26 	int i, sz, total = 0;
27 	char *name;
28 	char fullname[80];
29 
30 	for (i = 0; spufs_coredump_read[i].name != NULL; i++) {
31 		name = spufs_coredump_read[i].name;
32 		sz = spufs_coredump_read[i].size;
33 
34 		sprintf(fullname, "SPU/%d/%s", dfd, name);
35 
36 		total += sizeof(struct elf_note);
37 		total += roundup(strlen(fullname) + 1, 4);
38 		total += roundup(sz, 4);
39 	}
40 
41 	return total;
42 }
43 
match_context(const void * v,struct file * file,unsigned fd)44 static int match_context(const void *v, struct file *file, unsigned fd)
45 {
46 	struct spu_context *ctx;
47 	if (file->f_op != &spufs_context_fops)
48 		return 0;
49 	ctx = SPUFS_I(file_inode(file))->i_ctx;
50 	if (ctx->flags & SPU_CREATE_NOSCHED)
51 		return 0;
52 	return fd + 1;
53 }
54 
55 /*
56  * The additional architecture-specific notes for Cell are various
57  * context files in the spu context.
58  *
59  * This function iterates over all open file descriptors and sees
60  * if they are a directory in spufs.  In that case we use spufs
61  * internal functionality to dump them without needing to actually
62  * open the files.
63  */
64 /*
65  * descriptor table is not shared, so files can't change or go away.
66  */
coredump_next_context(int * fd)67 static struct spu_context *coredump_next_context(int *fd)
68 {
69 	struct spu_context *ctx;
70 	struct file *file;
71 	int n = iterate_fd(current->files, *fd, match_context, NULL);
72 	if (!n)
73 		return NULL;
74 	*fd = n - 1;
75 
76 	rcu_read_lock();
77 	file = lookup_fd_rcu(*fd);
78 	ctx = SPUFS_I(file_inode(file))->i_ctx;
79 	get_spu_context(ctx);
80 	rcu_read_unlock();
81 
82 	return ctx;
83 }
84 
spufs_coredump_extra_notes_size(void)85 int spufs_coredump_extra_notes_size(void)
86 {
87 	struct spu_context *ctx;
88 	int size = 0, rc, fd;
89 
90 	fd = 0;
91 	while ((ctx = coredump_next_context(&fd)) != NULL) {
92 		rc = spu_acquire_saved(ctx);
93 		if (rc) {
94 			put_spu_context(ctx);
95 			break;
96 		}
97 
98 		rc = spufs_ctx_note_size(ctx, fd);
99 		spu_release_saved(ctx);
100 		if (rc < 0) {
101 			put_spu_context(ctx);
102 			break;
103 		}
104 
105 		size += rc;
106 
107 		/* start searching the next fd next time */
108 		fd++;
109 		put_spu_context(ctx);
110 	}
111 
112 	return size;
113 }
114 
spufs_arch_write_note(struct spu_context * ctx,int i,struct coredump_params * cprm,int dfd)115 static int spufs_arch_write_note(struct spu_context *ctx, int i,
116 				  struct coredump_params *cprm, int dfd)
117 {
118 	size_t sz = spufs_coredump_read[i].size;
119 	char fullname[80];
120 	struct elf_note en;
121 	int ret;
122 
123 	sprintf(fullname, "SPU/%d/%s", dfd, spufs_coredump_read[i].name);
124 	en.n_namesz = strlen(fullname) + 1;
125 	en.n_descsz = sz;
126 	en.n_type = NT_SPU;
127 
128 	if (!dump_emit(cprm, &en, sizeof(en)))
129 		return -EIO;
130 	if (!dump_emit(cprm, fullname, en.n_namesz))
131 		return -EIO;
132 	if (!dump_align(cprm, 4))
133 		return -EIO;
134 
135 	if (spufs_coredump_read[i].dump) {
136 		ret = spufs_coredump_read[i].dump(ctx, cprm);
137 		if (ret < 0)
138 			return ret;
139 	} else {
140 		char buf[32];
141 
142 		ret = snprintf(buf, sizeof(buf), "0x%.16llx",
143 			       spufs_coredump_read[i].get(ctx));
144 		if (ret >= sizeof(buf))
145 			return sizeof(buf);
146 
147 		/* count trailing the NULL: */
148 		if (!dump_emit(cprm, buf, ret + 1))
149 			return -EIO;
150 	}
151 
152 	dump_skip_to(cprm, roundup(cprm->pos - ret + sz, 4));
153 	return 0;
154 }
155 
spufs_coredump_extra_notes_write(struct coredump_params * cprm)156 int spufs_coredump_extra_notes_write(struct coredump_params *cprm)
157 {
158 	struct spu_context *ctx;
159 	int fd, j, rc;
160 
161 	fd = 0;
162 	while ((ctx = coredump_next_context(&fd)) != NULL) {
163 		rc = spu_acquire_saved(ctx);
164 		if (rc)
165 			return rc;
166 
167 		for (j = 0; spufs_coredump_read[j].name != NULL; j++) {
168 			rc = spufs_arch_write_note(ctx, j, cprm, fd);
169 			if (rc) {
170 				spu_release_saved(ctx);
171 				return rc;
172 			}
173 		}
174 
175 		spu_release_saved(ctx);
176 
177 		/* start searching the next fd next time */
178 		fd++;
179 	}
180 
181 	return 0;
182 }
183