Passing the course of operating systems and trying to implement the paging memory algorithm. The algorithm has a limited number of pages ( NUMBER_PAGEBLE_PAGES = 33 ) and a mem_map_t structure containing a pointer to the first element of each page on the uint32_t *page .

Below is the code that is supposed to contain a local pointer that receives the address of the free page, and then, starting from the received address, performs a sequential initialization of the entire address space of this page with the value 0:

 static uint32_t *allocate_page(void) { uint32_t i = 0; uint32_t j = 0; uint32_t *page = NULL; printf("here 2\n"); while (i < NUM_PAGEABLE_PAGES) { if (m_map[i].in_use == 0) { m_map[i].in_use = 1; page = m_map[i].page; printf("here 3 %p\n", page); // Данная строка выдает segmentation fault 11 при инициализации ячейки памяти по указанному адресу. Почему? *page = 0; printf("here 4 %p\n", page); // данный "while-loop" пытается инициализировать все остальные ячейки в указанном while (j < 1024) { j++; printf("here 5\n"); page[j] = 0; printf("here 6\n"); } break; } i++; } return page; } Вывод в терминале следующий $ ./a.out here 2 here 3 0x100000 Segmentation fault: 11 

It turns out that the local pointer uint32_t *page gets the initial value of 0x100000, but when initializing the memory cell at the specified address, the value 0 gives an error.

Maybe there are ideas what is the matter here? Thank you in advance!

  • Error in handling foreign memory. The address is taken here: page = m_map[i].page; This means that you do not store allocated memory or insufficiently allocated memory. Is it really there that stands out 1025 ?? After all, you start the cycle with index 1! Try to put the iterations after accessing the memory, if it was not so strangely conceived. And do not put j to zero before the cycle. And this is intended? - AivanF.
  • @AivanF. Please post your comment as a response. - Nicolas Chabanovsky

2 answers 2

Error in handling foreign memory. The address is taken here: page = m_map[i].page; Hence the possible causes of the error:

  • Is it really there 1025 stands out? After all, you start index traversing with index 1, since j++; It is before accessing memory. Try to put after, if it was not so strangely conceived. By the way, it is better to make such numbers in constants.
  • You also do not set the value of j to zero before the cycle with the j cycle. Is this intended? If not, you need to fix it.

    As already indicated in my question, work is being done on the course “ Operating Systems ”, which involves developing its simple operating system from scratch, but the description of the question missed the point that all ( in this particular case ) address space must be manually initialized , without using standard C libraries . The specific task at this stage is to write a paging algorithm for paging .

    You were right. In the above program code containing the " allocate_page () " method, an error popped up when accessing someone else's address space . The question was to understand the reason for its occurrence, since the code above seems to be specifying the address of the variable "page = m_map [i] .page", which as a result means " page = 0x100000 ", and further, starting from this address Without allocating memory in advance, an attempt is made to initialize the memory cells of each subsequent address, namely " 0x100000, 0x100004, 0x100008, ..., 0x101000 (not including the last)" value "(uint32_t) 0 ".

    The error, as it turned out, was due to the fact that memory allocation using the library functions " malloc () " or " calloc () " in the implemented project is not provided, and the memory segments to which the request was in turn turned out to be occupied by others processes running on the machine on which the code was compiled. The project being implemented assumes the presence of an absolutely empty RAM, which is divided into segments (pages) in manual (manual) mode , in a certain sense the function " allocate_page () " given in the code is a kind of homemade prototype " malloc () " (or " calloc ( ) "). Therefore, in the algorithm described in my question after initializing the pointer " page = m_map [i] .page; ", (which results in page = 0x100000 ), when accessing the memory area at this address, a segmentation error was generated, since this area, like it is written a little higher, it is already used either by the operating system, or by some process in protected mode on the machine started by this code, which means that access to the selected segment of the address space is prohibited.

    When launched in the terminal, the code gives a segmentation error, and in the " Bochs x86-64 emulator " everything worked fine, everything was initialized as expected (meaning " p [0] = 0, p [1] = 0, ..., p [1023] = 0 ").

    As for the index " j " and the beginning of the " while-loop " with " j = 1 ", and not with " j = 0 ", these were all experiments to determine where the algorithm for initializing addresses in the address space " 0x10000 - 0x101000 " "" will stumble and give an error.

    Thanks for the discussion and advice, everything was interesting and to the point!