From 7dcea5fdafe66d8bcf1eeacbaf3f3f3b1c258dfc Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 31 Dec 2019 13:50:14 +0100 Subject: implement basic memory section allocation for processes --- paging.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'paging.h') diff --git a/paging.h b/paging.h index 1218fb8..1715a6e 100644 --- a/paging.h +++ b/paging.h @@ -3,4 +3,12 @@ void setup_flat_map(void); +void setup_pager_structures(uint32_t available_mem); + +#define CLAIM_FAILURE 0xffff + +// returns section number or CLAIM_FAILURE in case of failure +uint16_t claim_and_map_section +(void *owner, uint16_t where_to_map, uint8_t access_permissions); + #endif // PAGING_H -- cgit v1.2.3 From d2d7ebbea19d55fb2a292ca766d95b197691b941 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 31 Dec 2019 17:28:26 +0100 Subject: introduce SECTION_NULL --- demo_functionality.c | 2 +- paging.c | 30 +++++++++++++++++------------- paging.h | 4 ++-- 3 files changed, 20 insertions(+), 16 deletions(-) (limited to 'paging.h') diff --git a/demo_functionality.c b/demo_functionality.c index 366edd7..3cf5b8f 100644 --- a/demo_functionality.c +++ b/demo_functionality.c @@ -79,7 +79,7 @@ void demo_setup_PL0(void) uint16_t physical_section_number = claim_and_map_section ((void*) 5, PL0_SECTION_NUMBER, AP_2_0_MODEL_RW_ALL); - if (physical_section_number == CLAIM_FAILURE) + if (physical_section_number == SECTION_NULL) { puts("Couldn't claim memory section for unprivileged code :("); while(1); diff --git a/paging.c b/paging.c index 6560047..771c681 100644 --- a/paging.c +++ b/paging.c @@ -122,7 +122,7 @@ struct section_node void *owner; // 0 if free, 1 if used by kernel, 2 if split to pages // it's actually a 2-directional lists; - // end of list is marked by reference to itself; + // end of list is marked by reference to SECTION_NULL; // we use offsets into sections_list array instead of pointers; uint16_t prev, next; }; @@ -133,7 +133,7 @@ static uint16_t all_sections_count, kernel_sections_count, split_sections_count, free_sections_count; -// those are undefined when the corresponding count is 0; +// those are SECTION_NULL when the corresponding count is 0; static uint16_t first_free_section, first_kernel_section, first_split_section; @@ -146,11 +146,13 @@ void setup_pager_structures(uint32_t available_mem) sections_list = (struct section_node*) SECTIONS_LIST_START; + first_split_section = SECTION_NULL; + for (uint16_t i = 0; i < kernel_sections_count; i++) sections_list[i] = (struct section_node) { .owner = OWNER_KERNEL, - .prev = i == 0 ? i : i - 1, - .next = i == kernel_sections_count - 1 ? i : i + 1 + .prev = i == 0 ? SECTION_NULL : i - 1, + .next = i == kernel_sections_count - 1 ? SECTION_NULL : i + 1 }; first_kernel_section = 0; @@ -159,8 +161,8 @@ void setup_pager_structures(uint32_t available_mem) i < all_sections_count; i++) sections_list[i] = (struct section_node) { .owner = OWNER_FREE, - .prev = i == kernel_sections_count ? i : i - 1, - .next = i == all_sections_count - 1 ? i : i + 1 + .prev = i == kernel_sections_count ? SECTION_NULL : i - 1, + .next = i == all_sections_count - 1 ? SECTION_NULL : i + 1 }; first_free_section = kernel_sections_count; @@ -170,11 +172,11 @@ void setup_pager_structures(uint32_t available_mem) puts(" free sections left for use"); } -// return section number or CLAIM_FAILURE in case of failure +// return section number or SECTION_NULL in case of failure static uint16_t claim_section(void *owner) { if (!free_sections_count) - return CLAIM_FAILURE; // failure + return SECTION_NULL; // failure uint16_t section = first_free_section; @@ -183,10 +185,12 @@ static uint16_t claim_section(void *owner) uint16_t next; next = sections_list[section].next; - sections_list[next].prev = next; + sections_list[next].prev = SECTION_NULL; first_free_section = next; } + else + first_free_section = SECTION_NULL; if (owner == OWNER_KERNEL) { @@ -194,7 +198,7 @@ static uint16_t claim_section(void *owner) sections_list[section] = (struct section_node) { .owner = owner, - .prev = section, + .prev = SECTION_NULL, .next = first_kernel_section }; @@ -205,8 +209,8 @@ static uint16_t claim_section(void *owner) else sections_list[section] = (struct section_node) { .owner = owner, - .prev = section, - .next = section + .prev = SECTION_NULL, + .next = SECTION_NULL }; return section; @@ -218,7 +222,7 @@ uint16_t claim_and_map_section { uint16_t section = claim_section(owner); - if (section == CLAIM_FAILURE) + if (section == SECTION_NULL) return section; short_section_descriptor_t volatile *section_entry = diff --git a/paging.h b/paging.h index 1715a6e..4ac8efa 100644 --- a/paging.h +++ b/paging.h @@ -5,9 +5,9 @@ void setup_flat_map(void); void setup_pager_structures(uint32_t available_mem); -#define CLAIM_FAILURE 0xffff +#define SECTION_NULL 0xffff -// returns section number or CLAIM_FAILURE in case of failure +// returns section number or SECTION_NULL in case of failure uint16_t claim_and_map_section (void *owner, uint16_t where_to_map, uint8_t access_permissions); -- cgit v1.2.3