hibernate_64.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/init.h>
  14. #include <asm/proto.h>
  15. #include <asm/page.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/mtrr.h>
  18. #include <asm/suspend.h>
  19. /* References to section boundaries */
  20. extern const void __nosave_begin, __nosave_end;
  21. /* Defined in hibernate_asm_64.S */
  22. extern int restore_image(void);
  23. /*
  24. * Address to jump to in the last phase of restore in order to get to the image
  25. * kernel's text (this value is passed in the image header).
  26. */
  27. unsigned long restore_jump_address;
  28. /*
  29. * Value of the cr3 register from before the hibernation (this value is passed
  30. * in the image header).
  31. */
  32. unsigned long restore_cr3;
  33. pgd_t *temp_level4_pgt;
  34. void *relocated_restore_code;
  35. static void *alloc_pgt_page(void *context)
  36. {
  37. return (void *)get_safe_page(GFP_ATOMIC);
  38. }
  39. static int set_up_temporary_mappings(void)
  40. {
  41. struct x86_mapping_info info = {
  42. .alloc_pgt_page = alloc_pgt_page,
  43. .pmd_flag = __PAGE_KERNEL_LARGE_EXEC,
  44. .kernel_mapping = true,
  45. };
  46. unsigned long mstart, mend;
  47. int result;
  48. int i;
  49. temp_level4_pgt = (pgd_t *)get_safe_page(GFP_ATOMIC);
  50. if (!temp_level4_pgt)
  51. return -ENOMEM;
  52. /* It is safe to reuse the original kernel mapping */
  53. set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
  54. init_level4_pgt[pgd_index(__START_KERNEL_map)]);
  55. /* Set up the direct mapping from scratch */
  56. for (i = 0; i < nr_pfn_mapped; i++) {
  57. mstart = pfn_mapped[i].start << PAGE_SHIFT;
  58. mend = pfn_mapped[i].end << PAGE_SHIFT;
  59. result = kernel_ident_mapping_init(&info, temp_level4_pgt,
  60. mstart, mend);
  61. if (result)
  62. return result;
  63. }
  64. return 0;
  65. }
  66. int swsusp_arch_resume(void)
  67. {
  68. int error;
  69. /* We have got enough memory and from now on we cannot recover */
  70. if ((error = set_up_temporary_mappings()))
  71. return error;
  72. relocated_restore_code = (void *)get_safe_page(GFP_ATOMIC);
  73. if (!relocated_restore_code)
  74. return -ENOMEM;
  75. memcpy(relocated_restore_code, &core_restore_code,
  76. &restore_registers - &core_restore_code);
  77. restore_image();
  78. return 0;
  79. }
  80. /*
  81. * pfn_is_nosave - check if given pfn is in the 'nosave' section
  82. */
  83. int pfn_is_nosave(unsigned long pfn)
  84. {
  85. unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
  86. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
  87. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  88. }
  89. struct restore_data_record {
  90. unsigned long jump_address;
  91. unsigned long cr3;
  92. unsigned long magic;
  93. };
  94. #define RESTORE_MAGIC 0x0123456789ABCDEFUL
  95. /**
  96. * arch_hibernation_header_save - populate the architecture specific part
  97. * of a hibernation image header
  98. * @addr: address to save the data at
  99. */
  100. int arch_hibernation_header_save(void *addr, unsigned int max_size)
  101. {
  102. struct restore_data_record *rdr = addr;
  103. if (max_size < sizeof(struct restore_data_record))
  104. return -EOVERFLOW;
  105. rdr->jump_address = restore_jump_address;
  106. rdr->cr3 = restore_cr3;
  107. rdr->magic = RESTORE_MAGIC;
  108. return 0;
  109. }
  110. /**
  111. * arch_hibernation_header_restore - read the architecture specific data
  112. * from the hibernation image header
  113. * @addr: address to read the data from
  114. */
  115. int arch_hibernation_header_restore(void *addr)
  116. {
  117. struct restore_data_record *rdr = addr;
  118. restore_jump_address = rdr->jump_address;
  119. restore_cr3 = rdr->cr3;
  120. return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL;
  121. }