vmem.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2006
  4. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  5. */
  6. #include <linux/bootmem.h>
  7. #include <linux/pfn.h>
  8. #include <linux/mm.h>
  9. #include <linux/init.h>
  10. #include <linux/list.h>
  11. #include <linux/hugetlb.h>
  12. #include <linux/slab.h>
  13. #include <linux/memblock.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/pgalloc.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/setup.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/sections.h>
  20. #include <asm/set_memory.h>
  21. static DEFINE_MUTEX(vmem_mutex);
  22. struct memory_segment {
  23. struct list_head list;
  24. unsigned long start;
  25. unsigned long size;
  26. };
  27. static LIST_HEAD(mem_segs);
  28. static void __ref *vmem_alloc_pages(unsigned int order)
  29. {
  30. unsigned long size = PAGE_SIZE << order;
  31. if (slab_is_available())
  32. return (void *)__get_free_pages(GFP_KERNEL, order);
  33. return (void *) memblock_alloc(size, size);
  34. }
  35. void *vmem_crst_alloc(unsigned long val)
  36. {
  37. unsigned long *table;
  38. table = vmem_alloc_pages(CRST_ALLOC_ORDER);
  39. if (table)
  40. crst_table_init(table, val);
  41. return table;
  42. }
  43. pte_t __ref *vmem_pte_alloc(void)
  44. {
  45. unsigned long size = PTRS_PER_PTE * sizeof(pte_t);
  46. pte_t *pte;
  47. if (slab_is_available())
  48. pte = (pte_t *) page_table_alloc(&init_mm);
  49. else
  50. pte = (pte_t *) memblock_alloc(size, size);
  51. if (!pte)
  52. return NULL;
  53. memset64((u64 *)pte, _PAGE_INVALID, PTRS_PER_PTE);
  54. return pte;
  55. }
  56. /*
  57. * Add a physical memory range to the 1:1 mapping.
  58. */
  59. static int vmem_add_mem(unsigned long start, unsigned long size)
  60. {
  61. unsigned long pgt_prot, sgt_prot, r3_prot;
  62. unsigned long pages4k, pages1m, pages2g;
  63. unsigned long end = start + size;
  64. unsigned long address = start;
  65. pgd_t *pg_dir;
  66. p4d_t *p4_dir;
  67. pud_t *pu_dir;
  68. pmd_t *pm_dir;
  69. pte_t *pt_dir;
  70. int ret = -ENOMEM;
  71. pgt_prot = pgprot_val(PAGE_KERNEL);
  72. sgt_prot = pgprot_val(SEGMENT_KERNEL);
  73. r3_prot = pgprot_val(REGION3_KERNEL);
  74. if (!MACHINE_HAS_NX) {
  75. pgt_prot &= ~_PAGE_NOEXEC;
  76. sgt_prot &= ~_SEGMENT_ENTRY_NOEXEC;
  77. r3_prot &= ~_REGION_ENTRY_NOEXEC;
  78. }
  79. pages4k = pages1m = pages2g = 0;
  80. while (address < end) {
  81. pg_dir = pgd_offset_k(address);
  82. if (pgd_none(*pg_dir)) {
  83. p4_dir = vmem_crst_alloc(_REGION2_ENTRY_EMPTY);
  84. if (!p4_dir)
  85. goto out;
  86. pgd_populate(&init_mm, pg_dir, p4_dir);
  87. }
  88. p4_dir = p4d_offset(pg_dir, address);
  89. if (p4d_none(*p4_dir)) {
  90. pu_dir = vmem_crst_alloc(_REGION3_ENTRY_EMPTY);
  91. if (!pu_dir)
  92. goto out;
  93. p4d_populate(&init_mm, p4_dir, pu_dir);
  94. }
  95. pu_dir = pud_offset(p4_dir, address);
  96. if (MACHINE_HAS_EDAT2 && pud_none(*pu_dir) && address &&
  97. !(address & ~PUD_MASK) && (address + PUD_SIZE <= end) &&
  98. !debug_pagealloc_enabled()) {
  99. pud_val(*pu_dir) = address | r3_prot;
  100. address += PUD_SIZE;
  101. pages2g++;
  102. continue;
  103. }
  104. if (pud_none(*pu_dir)) {
  105. pm_dir = vmem_crst_alloc(_SEGMENT_ENTRY_EMPTY);
  106. if (!pm_dir)
  107. goto out;
  108. pud_populate(&init_mm, pu_dir, pm_dir);
  109. }
  110. pm_dir = pmd_offset(pu_dir, address);
  111. if (MACHINE_HAS_EDAT1 && pmd_none(*pm_dir) && address &&
  112. !(address & ~PMD_MASK) && (address + PMD_SIZE <= end) &&
  113. !debug_pagealloc_enabled()) {
  114. pmd_val(*pm_dir) = address | sgt_prot;
  115. address += PMD_SIZE;
  116. pages1m++;
  117. continue;
  118. }
  119. if (pmd_none(*pm_dir)) {
  120. pt_dir = vmem_pte_alloc();
  121. if (!pt_dir)
  122. goto out;
  123. pmd_populate(&init_mm, pm_dir, pt_dir);
  124. }
  125. pt_dir = pte_offset_kernel(pm_dir, address);
  126. pte_val(*pt_dir) = address | pgt_prot;
  127. address += PAGE_SIZE;
  128. pages4k++;
  129. }
  130. ret = 0;
  131. out:
  132. update_page_count(PG_DIRECT_MAP_4K, pages4k);
  133. update_page_count(PG_DIRECT_MAP_1M, pages1m);
  134. update_page_count(PG_DIRECT_MAP_2G, pages2g);
  135. return ret;
  136. }
  137. /*
  138. * Remove a physical memory range from the 1:1 mapping.
  139. * Currently only invalidates page table entries.
  140. */
  141. static void vmem_remove_range(unsigned long start, unsigned long size)
  142. {
  143. unsigned long pages4k, pages1m, pages2g;
  144. unsigned long end = start + size;
  145. unsigned long address = start;
  146. pgd_t *pg_dir;
  147. p4d_t *p4_dir;
  148. pud_t *pu_dir;
  149. pmd_t *pm_dir;
  150. pte_t *pt_dir;
  151. pages4k = pages1m = pages2g = 0;
  152. while (address < end) {
  153. pg_dir = pgd_offset_k(address);
  154. if (pgd_none(*pg_dir)) {
  155. address += PGDIR_SIZE;
  156. continue;
  157. }
  158. p4_dir = p4d_offset(pg_dir, address);
  159. if (p4d_none(*p4_dir)) {
  160. address += P4D_SIZE;
  161. continue;
  162. }
  163. pu_dir = pud_offset(p4_dir, address);
  164. if (pud_none(*pu_dir)) {
  165. address += PUD_SIZE;
  166. continue;
  167. }
  168. if (pud_large(*pu_dir)) {
  169. pud_clear(pu_dir);
  170. address += PUD_SIZE;
  171. pages2g++;
  172. continue;
  173. }
  174. pm_dir = pmd_offset(pu_dir, address);
  175. if (pmd_none(*pm_dir)) {
  176. address += PMD_SIZE;
  177. continue;
  178. }
  179. if (pmd_large(*pm_dir)) {
  180. pmd_clear(pm_dir);
  181. address += PMD_SIZE;
  182. pages1m++;
  183. continue;
  184. }
  185. pt_dir = pte_offset_kernel(pm_dir, address);
  186. pte_clear(&init_mm, address, pt_dir);
  187. address += PAGE_SIZE;
  188. pages4k++;
  189. }
  190. flush_tlb_kernel_range(start, end);
  191. update_page_count(PG_DIRECT_MAP_4K, -pages4k);
  192. update_page_count(PG_DIRECT_MAP_1M, -pages1m);
  193. update_page_count(PG_DIRECT_MAP_2G, -pages2g);
  194. }
  195. /*
  196. * Add a backed mem_map array to the virtual mem_map array.
  197. */
  198. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  199. struct vmem_altmap *altmap)
  200. {
  201. unsigned long pgt_prot, sgt_prot;
  202. unsigned long address = start;
  203. pgd_t *pg_dir;
  204. p4d_t *p4_dir;
  205. pud_t *pu_dir;
  206. pmd_t *pm_dir;
  207. pte_t *pt_dir;
  208. int ret = -ENOMEM;
  209. pgt_prot = pgprot_val(PAGE_KERNEL);
  210. sgt_prot = pgprot_val(SEGMENT_KERNEL);
  211. if (!MACHINE_HAS_NX) {
  212. pgt_prot &= ~_PAGE_NOEXEC;
  213. sgt_prot &= ~_SEGMENT_ENTRY_NOEXEC;
  214. }
  215. for (address = start; address < end;) {
  216. pg_dir = pgd_offset_k(address);
  217. if (pgd_none(*pg_dir)) {
  218. p4_dir = vmem_crst_alloc(_REGION2_ENTRY_EMPTY);
  219. if (!p4_dir)
  220. goto out;
  221. pgd_populate(&init_mm, pg_dir, p4_dir);
  222. }
  223. p4_dir = p4d_offset(pg_dir, address);
  224. if (p4d_none(*p4_dir)) {
  225. pu_dir = vmem_crst_alloc(_REGION3_ENTRY_EMPTY);
  226. if (!pu_dir)
  227. goto out;
  228. p4d_populate(&init_mm, p4_dir, pu_dir);
  229. }
  230. pu_dir = pud_offset(p4_dir, address);
  231. if (pud_none(*pu_dir)) {
  232. pm_dir = vmem_crst_alloc(_SEGMENT_ENTRY_EMPTY);
  233. if (!pm_dir)
  234. goto out;
  235. pud_populate(&init_mm, pu_dir, pm_dir);
  236. }
  237. pm_dir = pmd_offset(pu_dir, address);
  238. if (pmd_none(*pm_dir)) {
  239. /* Use 1MB frames for vmemmap if available. We always
  240. * use large frames even if they are only partially
  241. * used.
  242. * Otherwise we would have also page tables since
  243. * vmemmap_populate gets called for each section
  244. * separately. */
  245. if (MACHINE_HAS_EDAT1) {
  246. void *new_page;
  247. new_page = vmemmap_alloc_block(PMD_SIZE, node);
  248. if (!new_page)
  249. goto out;
  250. pmd_val(*pm_dir) = __pa(new_page) | sgt_prot;
  251. address = (address + PMD_SIZE) & PMD_MASK;
  252. continue;
  253. }
  254. pt_dir = vmem_pte_alloc();
  255. if (!pt_dir)
  256. goto out;
  257. pmd_populate(&init_mm, pm_dir, pt_dir);
  258. } else if (pmd_large(*pm_dir)) {
  259. address = (address + PMD_SIZE) & PMD_MASK;
  260. continue;
  261. }
  262. pt_dir = pte_offset_kernel(pm_dir, address);
  263. if (pte_none(*pt_dir)) {
  264. void *new_page;
  265. new_page = vmemmap_alloc_block(PAGE_SIZE, node);
  266. if (!new_page)
  267. goto out;
  268. pte_val(*pt_dir) = __pa(new_page) | pgt_prot;
  269. }
  270. address += PAGE_SIZE;
  271. }
  272. ret = 0;
  273. out:
  274. return ret;
  275. }
  276. void vmemmap_free(unsigned long start, unsigned long end,
  277. struct vmem_altmap *altmap)
  278. {
  279. }
  280. /*
  281. * Add memory segment to the segment list if it doesn't overlap with
  282. * an already present segment.
  283. */
  284. static int insert_memory_segment(struct memory_segment *seg)
  285. {
  286. struct memory_segment *tmp;
  287. if (seg->start + seg->size > VMEM_MAX_PHYS ||
  288. seg->start + seg->size < seg->start)
  289. return -ERANGE;
  290. list_for_each_entry(tmp, &mem_segs, list) {
  291. if (seg->start >= tmp->start + tmp->size)
  292. continue;
  293. if (seg->start + seg->size <= tmp->start)
  294. continue;
  295. return -ENOSPC;
  296. }
  297. list_add(&seg->list, &mem_segs);
  298. return 0;
  299. }
  300. /*
  301. * Remove memory segment from the segment list.
  302. */
  303. static void remove_memory_segment(struct memory_segment *seg)
  304. {
  305. list_del(&seg->list);
  306. }
  307. static void __remove_shared_memory(struct memory_segment *seg)
  308. {
  309. remove_memory_segment(seg);
  310. vmem_remove_range(seg->start, seg->size);
  311. }
  312. int vmem_remove_mapping(unsigned long start, unsigned long size)
  313. {
  314. struct memory_segment *seg;
  315. int ret;
  316. mutex_lock(&vmem_mutex);
  317. ret = -ENOENT;
  318. list_for_each_entry(seg, &mem_segs, list) {
  319. if (seg->start == start && seg->size == size)
  320. break;
  321. }
  322. if (seg->start != start || seg->size != size)
  323. goto out;
  324. ret = 0;
  325. __remove_shared_memory(seg);
  326. kfree(seg);
  327. out:
  328. mutex_unlock(&vmem_mutex);
  329. return ret;
  330. }
  331. int vmem_add_mapping(unsigned long start, unsigned long size)
  332. {
  333. struct memory_segment *seg;
  334. int ret;
  335. mutex_lock(&vmem_mutex);
  336. ret = -ENOMEM;
  337. seg = kzalloc(sizeof(*seg), GFP_KERNEL);
  338. if (!seg)
  339. goto out;
  340. seg->start = start;
  341. seg->size = size;
  342. ret = insert_memory_segment(seg);
  343. if (ret)
  344. goto out_free;
  345. ret = vmem_add_mem(start, size);
  346. if (ret)
  347. goto out_remove;
  348. goto out;
  349. out_remove:
  350. __remove_shared_memory(seg);
  351. out_free:
  352. kfree(seg);
  353. out:
  354. mutex_unlock(&vmem_mutex);
  355. return ret;
  356. }
  357. /*
  358. * map whole physical memory to virtual memory (identity mapping)
  359. * we reserve enough space in the vmalloc area for vmemmap to hotplug
  360. * additional memory segments.
  361. */
  362. void __init vmem_map_init(void)
  363. {
  364. struct memblock_region *reg;
  365. for_each_memblock(memory, reg)
  366. vmem_add_mem(reg->base, reg->size);
  367. __set_memory((unsigned long)_stext,
  368. (unsigned long)(_etext - _stext) >> PAGE_SHIFT,
  369. SET_MEMORY_RO | SET_MEMORY_X);
  370. __set_memory((unsigned long)_etext,
  371. (unsigned long)(__end_rodata - _etext) >> PAGE_SHIFT,
  372. SET_MEMORY_RO);
  373. __set_memory((unsigned long)_sinittext,
  374. (unsigned long)(_einittext - _sinittext) >> PAGE_SHIFT,
  375. SET_MEMORY_RO | SET_MEMORY_X);
  376. pr_info("Write protected kernel read-only data: %luk\n",
  377. (unsigned long)(__end_rodata - _stext) >> 10);
  378. }
  379. /*
  380. * Convert memblock.memory to a memory segment list so there is a single
  381. * list that contains all memory segments.
  382. */
  383. static int __init vmem_convert_memory_chunk(void)
  384. {
  385. struct memblock_region *reg;
  386. struct memory_segment *seg;
  387. mutex_lock(&vmem_mutex);
  388. for_each_memblock(memory, reg) {
  389. seg = kzalloc(sizeof(*seg), GFP_KERNEL);
  390. if (!seg)
  391. panic("Out of memory...\n");
  392. seg->start = reg->base;
  393. seg->size = reg->size;
  394. insert_memory_segment(seg);
  395. }
  396. mutex_unlock(&vmem_mutex);
  397. return 0;
  398. }
  399. core_initcall(vmem_convert_memory_chunk);