hibernate_64.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Hibernation support for x86-64
  3. *
  4. * Distribute under GPLv2
  5. *
  6. * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
  7. * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
  8. * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
  9. */
  10. #include <linux/gfp.h>
  11. #include <linux/smp.h>
  12. #include <linux/suspend.h>
  13. #include <asm/proto.h>
  14. #include <asm/page.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/mtrr.h>
  17. #include <asm/suspend.h>
  18. /* References to section boundaries */
  19. extern const void __nosave_begin, __nosave_end;
  20. /* Defined in hibernate_asm_64.S */
  21. extern int restore_image(void);
  22. /*
  23. * Address to jump to in the last phase of restore in order to get to the image
  24. * kernel's text (this value is passed in the image header).
  25. */
  26. unsigned long restore_jump_address;
  27. /*
  28. * Value of the cr3 register from before the hibernation (this value is passed
  29. * in the image header).
  30. */
  31. unsigned long restore_cr3;
  32. pgd_t *temp_level4_pgt;
  33. void *relocated_restore_code;
  34. static int res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
  35. {
  36. long i, j;
  37. i = pud_index(address);
  38. pud = pud + i;
  39. for (; i < PTRS_PER_PUD; pud++, i++) {
  40. unsigned long paddr;
  41. pmd_t *pmd;
  42. paddr = address + i*PUD_SIZE;
  43. if (paddr >= end)
  44. break;
  45. pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
  46. if (!pmd)
  47. return -ENOMEM;
  48. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
  49. for (j = 0; j < PTRS_PER_PMD; pmd++, j++, paddr += PMD_SIZE) {
  50. unsigned long pe;
  51. if (paddr >= end)
  52. break;
  53. pe = __PAGE_KERNEL_LARGE_EXEC | paddr;
  54. pe &= __supported_pte_mask;
  55. set_pmd(pmd, __pmd(pe));
  56. }
  57. }
  58. return 0;
  59. }
  60. static int set_up_temporary_mappings(void)
  61. {
  62. unsigned long start, end, next;
  63. int error;
  64. temp_level4_pgt = (pgd_t *)get_safe_page(GFP_ATOMIC);
  65. if (!temp_level4_pgt)
  66. return -ENOMEM;
  67. /* It is safe to reuse the original kernel mapping */
  68. set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
  69. init_level4_pgt[pgd_index(__START_KERNEL_map)]);
  70. /* Set up the direct mapping from scratch */
  71. start = (unsigned long)pfn_to_kaddr(0);
  72. end = (unsigned long)pfn_to_kaddr(max_pfn);
  73. for (; start < end; start = next) {
  74. pud_t *pud = (pud_t *)get_safe_page(GFP_ATOMIC);
  75. if (!pud)
  76. return -ENOMEM;
  77. next = start + PGDIR_SIZE;
  78. if (next > end)
  79. next = end;
  80. if ((error = res_phys_pud_init(pud, __pa(start), __pa(next))))
  81. return error;
  82. set_pgd(temp_level4_pgt + pgd_index(start),
  83. mk_kernel_pgd(__pa(pud)));
  84. }
  85. return 0;
  86. }
  87. int swsusp_arch_resume(void)
  88. {
  89. int error;
  90. /* We have got enough memory and from now on we cannot recover */
  91. if ((error = set_up_temporary_mappings()))
  92. return error;
  93. relocated_restore_code = (void *)get_safe_page(GFP_ATOMIC);
  94. if (!relocated_restore_code)
  95. return -ENOMEM;
  96. memcpy(relocated_restore_code, &core_restore_code,
  97. &restore_registers - &core_restore_code);
  98. restore_image();
  99. return 0;
  100. }
  101. /*
  102. * pfn_is_nosave - check if given pfn is in the 'nosave' section
  103. */
  104. int pfn_is_nosave(unsigned long pfn)
  105. {
  106. unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
  107. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
  108. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  109. }
  110. struct restore_data_record {
  111. unsigned long jump_address;
  112. unsigned long cr3;
  113. unsigned long magic;
  114. };
  115. #define RESTORE_MAGIC 0x0123456789ABCDEFUL
  116. /**
  117. * arch_hibernation_header_save - populate the architecture specific part
  118. * of a hibernation image header
  119. * @addr: address to save the data at
  120. */
  121. int arch_hibernation_header_save(void *addr, unsigned int max_size)
  122. {
  123. struct restore_data_record *rdr = addr;
  124. if (max_size < sizeof(struct restore_data_record))
  125. return -EOVERFLOW;
  126. rdr->jump_address = restore_jump_address;
  127. rdr->cr3 = restore_cr3;
  128. rdr->magic = RESTORE_MAGIC;
  129. return 0;
  130. }
  131. /**
  132. * arch_hibernation_header_restore - read the architecture specific data
  133. * from the hibernation image header
  134. * @addr: address to read the data from
  135. */
  136. int arch_hibernation_header_restore(void *addr)
  137. {
  138. struct restore_data_record *rdr = addr;
  139. restore_jump_address = rdr->jump_address;
  140. restore_cr3 = rdr->cr3;
  141. return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL;
  142. }