coredump.c 4.7 KB

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