arceos-org / arceos

An experimental modular OS written in Rust.
https://arceos.org/arceos/
Apache License 2.0
503 stars 254 forks source link

Add DMA alloc support to dev branch #165

Closed ZR233 closed 2 months ago

ZR233 commented 2 months ago

In the context of ArceOS, when handling paging, all free regions were initially configured with caching attributes, which posed a challenge for DMA (Direct Memory Access) memory allocation during driver development where non-cached memory was required. The current modification approach involves:

  1. On-demand Page Table Allocation: Instead of setting up all page table entries at boot time, the system now dynamically allocates necessary page table entries as they are needed. This strategy allows for more flexibility in configuring memory attributes at the time of memory allocation.

  2. DMA Allocation with Non-Caching Attributes: When allocating DMA memory, if the existing page table space is insufficient, new page table entries are set up at this point, enabling the configuration of non-caching attributes. This ensures that DMA operations can access memory without the overhead of caching, which is critical for performance and integrity in certain scenarios.

  3. Bytes Allocator Space Management: If the bytes allocator does not have enough available space to fulfill a memory request, it first identifies a segment of physical memory using the page allocator. Then, it configures the page table entries for this physical address range according to the required memory attributes (in this case, non-caching). Once these entries are set up, the newly allocated and configured memory segment is added to the bytes allocator pool. Subsequently, the bytes allocator can use this memory to satisfy the original allocation request.

This approach optimizes memory management by dynamically adjusting to the specific needs of different memory allocations, particularly those requiring non-cached properties, such as DMA operations. It enhances system efficiency and resource utilization by minimizing unnecessary caching where it's not beneficial.