os_info.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * OS info memory interface
  4. *
  5. * Copyright IBM Corp. 2012
  6. * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "os_info"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/crash_dump.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <asm/checksum.h>
  14. #include <asm/lowcore.h>
  15. #include <asm/os_info.h>
  16. /*
  17. * OS info structure has to be page aligned
  18. */
  19. static struct os_info os_info __page_aligned_data;
  20. /*
  21. * Compute checksum over OS info structure
  22. */
  23. u32 os_info_csum(struct os_info *os_info)
  24. {
  25. int size = sizeof(*os_info) - offsetof(struct os_info, version_major);
  26. return (__force u32)csum_partial(&os_info->version_major, size, 0);
  27. }
  28. /*
  29. * Add crashkernel info to OS info and update checksum
  30. */
  31. void os_info_crashkernel_add(unsigned long base, unsigned long size)
  32. {
  33. os_info.crashkernel_addr = (u64)(unsigned long)base;
  34. os_info.crashkernel_size = (u64)(unsigned long)size;
  35. os_info.csum = os_info_csum(&os_info);
  36. }
  37. /*
  38. * Add OS info entry and update checksum
  39. */
  40. void os_info_entry_add(int nr, void *ptr, u64 size)
  41. {
  42. os_info.entry[nr].addr = (u64)(unsigned long)ptr;
  43. os_info.entry[nr].size = size;
  44. os_info.entry[nr].csum = (__force u32)csum_partial(ptr, size, 0);
  45. os_info.csum = os_info_csum(&os_info);
  46. }
  47. /*
  48. * Initialize OS info struture and set lowcore pointer
  49. */
  50. void __init os_info_init(void)
  51. {
  52. void *ptr = &os_info;
  53. os_info.version_major = OS_INFO_VERSION_MAJOR;
  54. os_info.version_minor = OS_INFO_VERSION_MINOR;
  55. os_info.magic = OS_INFO_MAGIC;
  56. os_info.csum = os_info_csum(&os_info);
  57. mem_assign_absolute(S390_lowcore.os_info, (unsigned long) ptr);
  58. }
  59. #ifdef CONFIG_CRASH_DUMP
  60. static struct os_info *os_info_old;
  61. /*
  62. * Allocate and copy OS info entry from oldmem
  63. */
  64. static void os_info_old_alloc(int nr, int align)
  65. {
  66. unsigned long addr, size = 0;
  67. char *buf, *buf_align, *msg;
  68. u32 csum;
  69. addr = os_info_old->entry[nr].addr;
  70. if (!addr) {
  71. msg = "not available";
  72. goto fail;
  73. }
  74. size = os_info_old->entry[nr].size;
  75. buf = kmalloc(size + align - 1, GFP_KERNEL);
  76. if (!buf) {
  77. msg = "alloc failed";
  78. goto fail;
  79. }
  80. buf_align = PTR_ALIGN(buf, align);
  81. if (copy_oldmem_kernel(buf_align, (void *) addr, size)) {
  82. msg = "copy failed";
  83. goto fail_free;
  84. }
  85. csum = (__force u32)csum_partial(buf_align, size, 0);
  86. if (csum != os_info_old->entry[nr].csum) {
  87. msg = "checksum failed";
  88. goto fail_free;
  89. }
  90. os_info_old->entry[nr].addr = (u64)(unsigned long)buf_align;
  91. msg = "copied";
  92. goto out;
  93. fail_free:
  94. kfree(buf);
  95. fail:
  96. os_info_old->entry[nr].addr = 0;
  97. out:
  98. pr_info("entry %i: %s (addr=0x%lx size=%lu)\n",
  99. nr, msg, addr, size);
  100. }
  101. /*
  102. * Initialize os info and os info entries from oldmem
  103. */
  104. static void os_info_old_init(void)
  105. {
  106. static int os_info_init;
  107. unsigned long addr;
  108. if (os_info_init)
  109. return;
  110. if (!OLDMEM_BASE)
  111. goto fail;
  112. if (copy_oldmem_kernel(&addr, &S390_lowcore.os_info, sizeof(addr)))
  113. goto fail;
  114. if (addr == 0 || addr % PAGE_SIZE)
  115. goto fail;
  116. os_info_old = kzalloc(sizeof(*os_info_old), GFP_KERNEL);
  117. if (!os_info_old)
  118. goto fail;
  119. if (copy_oldmem_kernel(os_info_old, (void *) addr,
  120. sizeof(*os_info_old)))
  121. goto fail_free;
  122. if (os_info_old->magic != OS_INFO_MAGIC)
  123. goto fail_free;
  124. if (os_info_old->csum != os_info_csum(os_info_old))
  125. goto fail_free;
  126. if (os_info_old->version_major > OS_INFO_VERSION_MAJOR)
  127. goto fail_free;
  128. os_info_old_alloc(OS_INFO_VMCOREINFO, 1);
  129. os_info_old_alloc(OS_INFO_REIPL_BLOCK, 1);
  130. pr_info("crashkernel: addr=0x%lx size=%lu\n",
  131. (unsigned long) os_info_old->crashkernel_addr,
  132. (unsigned long) os_info_old->crashkernel_size);
  133. os_info_init = 1;
  134. return;
  135. fail_free:
  136. kfree(os_info_old);
  137. fail:
  138. os_info_init = 1;
  139. os_info_old = NULL;
  140. }
  141. /*
  142. * Return pointer to os infor entry and its size
  143. */
  144. void *os_info_old_entry(int nr, unsigned long *size)
  145. {
  146. os_info_old_init();
  147. if (!os_info_old)
  148. return NULL;
  149. if (!os_info_old->entry[nr].addr)
  150. return NULL;
  151. *size = (unsigned long) os_info_old->entry[nr].size;
  152. return (void *)(unsigned long)os_info_old->entry[nr].addr;
  153. }
  154. #endif