hibernate_64.c 4.0 KB

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