os_info.c 3.9 KB

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