mm.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* include/kernel/mm.h
  2. *
  3. * This file is part of evx.
  4. *
  5. * evx is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * evx is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with evx. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #ifndef __MM_H
  19. #define __MM_H
  20. #ifndef __ASSEMBLER__
  21. #include <sys/types.h>
  22. #include <lib/klist.h>
  23. #include <arch/archinfo.h>
  24. /* a descriptor for each page to describe a page, hopely be smallest
  25. * and this is create only for the buddy memory management system. we
  26. * can't implement that without this, because we need more memory when
  27. * inserting.
  28. *
  29. * we should initiallize this as late as we can, after maps okay.
  30. * */
  31. struct page {
  32. /* for cow, the reference count. we won't free if count--
  33. * id of this page, can let freepage faster */
  34. __uint32_t info;
  35. #define page_count(__p) ((__p)->info & 0xffu)
  36. #define page_id(__p) ((__p)->info >> 24u)
  37. struct page *next;
  38. };
  39. /* okay, that's may only in the x86, but... */
  40. struct zone {
  41. __uint32_t total_pages;
  42. #define ZONETYPE_RAM 1
  43. #define ZONETYPE_OTHER 0
  44. __uint32_t type;
  45. void *start_addr;
  46. struct page *pages;
  47. };
  48. /* the bucket_entry are for free objects. next_free points to next free
  49. * object. we replace the freeptr pointer in struct mempool and let that
  50. * points to old freeptr */
  51. struct bucket_entry {
  52. struct bucket_entry *next;
  53. };
  54. #define CACHE_SIZE 4096
  55. struct mempool {
  56. int size, used;
  57. void *cache;
  58. struct bucket_entry *freeptr;
  59. /* it has the same size */
  60. struct mempool *next_pool;
  61. };
  62. struct mempool_dir {
  63. int size;
  64. struct mempool *pools;
  65. };
  66. void *__mempool_alloc(struct mempool *);
  67. int __mempool_free(struct mempool *, void *);
  68. void *kmalloc(int size);
  69. int free_s(void *p, int size);
  70. int kmalloc_report(void);
  71. int dump_pool(struct mempool_dir *);
  72. void *early_kmalloc(int nbytes, int align);
  73. int early_free(void *p, int nbytes);
  74. struct page *getfreepage(int order, int gfp_mask);
  75. int freepage(struct page *, int order);
  76. #define free(p) early_free(p, 0)
  77. #define BUDDY_LEVELS 5
  78. extern struct klist_node buddy_pages[BUDDY_LEVELS];
  79. /* nr_ram_pages is the total */
  80. extern int nr_ram_pages, usable_ram_pages;
  81. /* sets by arch_mm_init */
  82. extern struct page *first_usable_ram,
  83. *kmem_pages;
  84. #define kmem_zones ((struct zone *) kernel_end)
  85. /* they're really early but fast. actually, most free() did nothing
  86. *
  87. * WARNING that you should't use align, because we didn't save old
  88. * freeptr and it will be cracked by it: not freeable */
  89. #endif
  90. #endif