hibernate_64.c 3.5 KB

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