@@ -57,12 +57,20 @@ bool vMemory::fork_reset(const Machine& main_vm, const MachineOptions& options)
57
57
// from the master VM to this forked VM. This is a gamble
58
58
// that it's cheaper to copy than the TLB flushes that happen
59
59
// from the mov cr3.
60
- // / XXX: TODO: If we are resetting with a lower working memory than
61
- // / what is currently allocated, we cannot re-use the memory banks
62
- // / anymore and we have to do a full reset. Although, we could have
63
- // / a rule that says if you are above the limit, it just means you
64
- // / have nowhere to go. If you need one more page, a fault will be
65
- // / triggered which again triggers a full reset. Oh well.
60
+ if (options.reset_free_work_mem != 0 ) {
61
+ // When reset_free_work_mem is non-zero, we will compare the
62
+ // memory bank working memory usage against the limit.
63
+ // If the limit is exceeded, we will return true to indicate
64
+ // that a full reset is to be performed, which will release
65
+ // memory back to the system, keeping memory usage in check.
66
+ const uint64_t used = this ->machine .banked_memory_pages () *
67
+ vMemory::PageSize ();
68
+ if (used > uint64_t (options.reset_free_work_mem )) {
69
+ // fprintf(stderr, "Freeing %zu bytes of work memory\n", used);
70
+ this ->banks .reset (options);
71
+ return true ;
72
+ }
73
+ }
66
74
try {
67
75
foreach_page (*this , [&](uint64_t addr, uint64_t & entry, uint64_t page_size) {
68
76
// If the page is writable, we will restore the original
0 commit comments