zcore.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * zcore module to export memory content and register sets for creating system
  3. * dumps on SCSI disks (zfcpdump). The "zcore/mem" debugfs file shows the same
  4. * dump format as s390 standalone dumps.
  5. *
  6. * For more information please refer to Documentation/s390/zfcpdump.txt
  7. *
  8. * Copyright IBM Corp. 2003, 2008
  9. * Author(s): Michael Holzheu
  10. */
  11. #define KMSG_COMPONENT "zdump"
  12. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/module.h>
  18. #include <linux/memblock.h>
  19. #include <asm/asm-offsets.h>
  20. #include <asm/ipl.h>
  21. #include <asm/sclp.h>
  22. #include <asm/setup.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/debug.h>
  25. #include <asm/processor.h>
  26. #include <asm/irqflags.h>
  27. #include <asm/checksum.h>
  28. #include <asm/os_info.h>
  29. #include <asm/switch_to.h>
  30. #include "sclp.h"
  31. #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
  32. #define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
  33. enum arch_id {
  34. ARCH_S390 = 0,
  35. ARCH_S390X = 1,
  36. };
  37. struct ipib_info {
  38. unsigned long ipib;
  39. u32 checksum;
  40. } __attribute__((packed));
  41. static struct debug_info *zcore_dbf;
  42. static int hsa_available;
  43. static struct dentry *zcore_dir;
  44. static struct dentry *zcore_memmap_file;
  45. static struct dentry *zcore_reipl_file;
  46. static struct dentry *zcore_hsa_file;
  47. static struct ipl_parameter_block *ipl_block;
  48. static char hsa_buf[PAGE_SIZE] __aligned(PAGE_SIZE);
  49. /*
  50. * Copy memory from HSA to user memory (not reentrant):
  51. *
  52. * @dest: User buffer where memory should be copied to
  53. * @src: Start address within HSA where data should be copied
  54. * @count: Size of buffer, which should be copied
  55. */
  56. int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
  57. {
  58. unsigned long offset, bytes;
  59. if (!hsa_available)
  60. return -ENODATA;
  61. while (count) {
  62. if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
  63. TRACE("sclp_sdias_copy() failed\n");
  64. return -EIO;
  65. }
  66. offset = src % PAGE_SIZE;
  67. bytes = min(PAGE_SIZE - offset, count);
  68. if (copy_to_user(dest, hsa_buf + offset, bytes))
  69. return -EFAULT;
  70. src += bytes;
  71. dest += bytes;
  72. count -= bytes;
  73. }
  74. return 0;
  75. }
  76. /*
  77. * Copy memory from HSA to kernel memory (not reentrant):
  78. *
  79. * @dest: Kernel or user buffer where memory should be copied to
  80. * @src: Start address within HSA where data should be copied
  81. * @count: Size of buffer, which should be copied
  82. */
  83. int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
  84. {
  85. unsigned long offset, bytes;
  86. if (!hsa_available)
  87. return -ENODATA;
  88. while (count) {
  89. if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
  90. TRACE("sclp_sdias_copy() failed\n");
  91. return -EIO;
  92. }
  93. offset = src % PAGE_SIZE;
  94. bytes = min(PAGE_SIZE - offset, count);
  95. memcpy(dest, hsa_buf + offset, bytes);
  96. src += bytes;
  97. dest += bytes;
  98. count -= bytes;
  99. }
  100. return 0;
  101. }
  102. static int __init init_cpu_info(void)
  103. {
  104. struct save_area *sa;
  105. /* get info for boot cpu from lowcore, stored in the HSA */
  106. sa = save_area_boot_cpu();
  107. if (!sa)
  108. return -ENOMEM;
  109. if (memcpy_hsa_kernel(hsa_buf, __LC_FPREGS_SAVE_AREA, 512) < 0) {
  110. TRACE("could not copy from HSA\n");
  111. return -EIO;
  112. }
  113. save_area_add_regs(sa, hsa_buf); /* vx registers are saved in smp.c */
  114. return 0;
  115. }
  116. /*
  117. * Release the HSA
  118. */
  119. static void release_hsa(void)
  120. {
  121. diag308(DIAG308_REL_HSA, NULL);
  122. hsa_available = 0;
  123. }
  124. static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
  125. size_t count, loff_t *ppos)
  126. {
  127. return simple_read_from_buffer(buf, count, ppos, filp->private_data,
  128. memblock.memory.cnt * CHUNK_INFO_SIZE);
  129. }
  130. static int zcore_memmap_open(struct inode *inode, struct file *filp)
  131. {
  132. struct memblock_region *reg;
  133. char *buf;
  134. int i = 0;
  135. buf = kzalloc(memblock.memory.cnt * CHUNK_INFO_SIZE, GFP_KERNEL);
  136. if (!buf) {
  137. return -ENOMEM;
  138. }
  139. for_each_memblock(memory, reg) {
  140. sprintf(buf + (i++ * CHUNK_INFO_SIZE), "%016llx %016llx ",
  141. (unsigned long long) reg->base,
  142. (unsigned long long) reg->size);
  143. }
  144. filp->private_data = buf;
  145. return nonseekable_open(inode, filp);
  146. }
  147. static int zcore_memmap_release(struct inode *inode, struct file *filp)
  148. {
  149. kfree(filp->private_data);
  150. return 0;
  151. }
  152. static const struct file_operations zcore_memmap_fops = {
  153. .owner = THIS_MODULE,
  154. .read = zcore_memmap_read,
  155. .open = zcore_memmap_open,
  156. .release = zcore_memmap_release,
  157. .llseek = no_llseek,
  158. };
  159. static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
  160. size_t count, loff_t *ppos)
  161. {
  162. if (ipl_block) {
  163. diag308(DIAG308_SET, ipl_block);
  164. diag308(DIAG308_LOAD_CLEAR, NULL);
  165. }
  166. return count;
  167. }
  168. static int zcore_reipl_open(struct inode *inode, struct file *filp)
  169. {
  170. return nonseekable_open(inode, filp);
  171. }
  172. static int zcore_reipl_release(struct inode *inode, struct file *filp)
  173. {
  174. return 0;
  175. }
  176. static const struct file_operations zcore_reipl_fops = {
  177. .owner = THIS_MODULE,
  178. .write = zcore_reipl_write,
  179. .open = zcore_reipl_open,
  180. .release = zcore_reipl_release,
  181. .llseek = no_llseek,
  182. };
  183. static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
  184. size_t count, loff_t *ppos)
  185. {
  186. static char str[18];
  187. if (hsa_available)
  188. snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
  189. else
  190. snprintf(str, sizeof(str), "0\n");
  191. return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
  192. }
  193. static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
  194. size_t count, loff_t *ppos)
  195. {
  196. char value;
  197. if (*ppos != 0)
  198. return -EPIPE;
  199. if (copy_from_user(&value, buf, 1))
  200. return -EFAULT;
  201. if (value != '0')
  202. return -EINVAL;
  203. release_hsa();
  204. return count;
  205. }
  206. static const struct file_operations zcore_hsa_fops = {
  207. .owner = THIS_MODULE,
  208. .write = zcore_hsa_write,
  209. .read = zcore_hsa_read,
  210. .open = nonseekable_open,
  211. .llseek = no_llseek,
  212. };
  213. static int __init check_sdias(void)
  214. {
  215. if (!sclp.hsa_size) {
  216. TRACE("Could not determine HSA size\n");
  217. return -ENODEV;
  218. }
  219. return 0;
  220. }
  221. /*
  222. * Provide IPL parameter information block from either HSA or memory
  223. * for future reipl
  224. */
  225. static int __init zcore_reipl_init(void)
  226. {
  227. struct ipib_info ipib_info;
  228. int rc;
  229. rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
  230. if (rc)
  231. return rc;
  232. if (ipib_info.ipib == 0)
  233. return 0;
  234. ipl_block = (void *) __get_free_page(GFP_KERNEL);
  235. if (!ipl_block)
  236. return -ENOMEM;
  237. if (ipib_info.ipib < sclp.hsa_size)
  238. rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
  239. else
  240. rc = memcpy_real(ipl_block, (void *) ipib_info.ipib, PAGE_SIZE);
  241. if (rc || csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
  242. ipib_info.checksum) {
  243. TRACE("Checksum does not match\n");
  244. free_page((unsigned long) ipl_block);
  245. ipl_block = NULL;
  246. }
  247. return 0;
  248. }
  249. static int __init zcore_init(void)
  250. {
  251. unsigned char arch;
  252. int rc;
  253. if (ipl_info.type != IPL_TYPE_FCP_DUMP)
  254. return -ENODATA;
  255. if (OLDMEM_BASE)
  256. return -ENODATA;
  257. zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
  258. debug_register_view(zcore_dbf, &debug_sprintf_view);
  259. debug_set_level(zcore_dbf, 6);
  260. TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
  261. TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
  262. TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
  263. rc = sclp_sdias_init();
  264. if (rc)
  265. goto fail;
  266. rc = check_sdias();
  267. if (rc)
  268. goto fail;
  269. hsa_available = 1;
  270. rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
  271. if (rc)
  272. goto fail;
  273. if (arch == ARCH_S390) {
  274. pr_alert("The 64-bit dump tool cannot be used for a "
  275. "32-bit system\n");
  276. rc = -EINVAL;
  277. goto fail;
  278. }
  279. pr_alert("DETECTED 'S390X (64 bit) OS'\n");
  280. rc = init_cpu_info();
  281. if (rc)
  282. goto fail;
  283. rc = zcore_reipl_init();
  284. if (rc)
  285. goto fail;
  286. zcore_dir = debugfs_create_dir("zcore" , NULL);
  287. if (!zcore_dir) {
  288. rc = -ENOMEM;
  289. goto fail;
  290. }
  291. zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
  292. NULL, &zcore_memmap_fops);
  293. if (!zcore_memmap_file) {
  294. rc = -ENOMEM;
  295. goto fail_dir;
  296. }
  297. zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
  298. NULL, &zcore_reipl_fops);
  299. if (!zcore_reipl_file) {
  300. rc = -ENOMEM;
  301. goto fail_memmap_file;
  302. }
  303. zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
  304. NULL, &zcore_hsa_fops);
  305. if (!zcore_hsa_file) {
  306. rc = -ENOMEM;
  307. goto fail_reipl_file;
  308. }
  309. return 0;
  310. fail_reipl_file:
  311. debugfs_remove(zcore_reipl_file);
  312. fail_memmap_file:
  313. debugfs_remove(zcore_memmap_file);
  314. fail_dir:
  315. debugfs_remove(zcore_dir);
  316. fail:
  317. diag308(DIAG308_REL_HSA, NULL);
  318. return rc;
  319. }
  320. static void __exit zcore_exit(void)
  321. {
  322. debug_unregister(zcore_dbf);
  323. sclp_sdias_exit();
  324. free_page((unsigned long) ipl_block);
  325. debugfs_remove(zcore_hsa_file);
  326. debugfs_remove(zcore_reipl_file);
  327. debugfs_remove(zcore_memmap_file);
  328. debugfs_remove(zcore_dir);
  329. diag308(DIAG308_REL_HSA, NULL);
  330. }
  331. MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
  332. MODULE_DESCRIPTION("zcore module for zfcpdump support");
  333. MODULE_LICENSE("GPL");
  334. subsys_initcall(zcore_init);
  335. module_exit(zcore_exit);