hibernate_64.c 4.1 KB

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