From 0b2b263060907e8bf732c1d27c66b7358af01e9f Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 5 Sep 2021 18:55:32 +0200 Subject: make virt_iter support iterating over physical memory too --- src/kernel/mem/virt.c | 22 +++++++++++++--------- src/kernel/mem/virt.h | 1 + 2 files changed, 14 insertions(+), 9 deletions(-) (limited to 'src/kernel/mem') diff --git a/src/kernel/mem/virt.c b/src/kernel/mem/virt.c index 2990668..beeb54f 100644 --- a/src/kernel/mem/virt.c +++ b/src/kernel/mem/virt.c @@ -23,21 +23,25 @@ bool virt_iter_next(struct virt_iter *iter) { * virtual and physical memory, which might not always be the case. * TODO test this */ - user_ptr virt = iter->_virt; size_t partial = iter->_remaining; iter->prior += iter->frag_len; if (partial <= 0) return false; - // don't read past the page - if ((virt & PAGE_MASK) + partial > PAGE_SIZE) - partial = PAGE_SIZE - (virt & PAGE_MASK); + if (iter->_pages) { // if iterating over virtual memory + // don't read past the page + if ((iter->_virt & PAGE_MASK) + partial > PAGE_SIZE) + partial = PAGE_SIZE - (iter->_virt & PAGE_MASK); - iter->frag = pagedir_virt2phys(iter->_pages, - iter->_virt, iter->_user, iter->_writeable); + iter->frag = pagedir_virt2phys(iter->_pages, + iter->_virt, iter->_user, iter->_writeable); - if (iter->frag == 0) { - iter->error = true; - return false; + if (iter->frag == 0) { + iter->error = true; + return false; + } + } else { + // "iterate" over physical memory + iter->frag = iter->_virt; } iter->frag_len = partial; diff --git a/src/kernel/mem/virt.h b/src/kernel/mem/virt.h index 98a7a16..e87a9fe 100644 --- a/src/kernel/mem/virt.h +++ b/src/kernel/mem/virt.h @@ -17,6 +17,7 @@ struct virt_iter { bool _writeable; }; +/* if pages == NULL, create an iterator over physical memory. */ void virt_iter_new( struct virt_iter *iter, user_ptr virt, size_t length, struct pagedir *pages, bool user, bool writeable); -- cgit v1.2.3