Author Topic: When does restarting instruction becomes difficult after page fault?  (Read 998 times)

0 Members and 1 Guest are viewing this topic.

Offline shivajikobardanTopic starter

  • Regular Contributor
  • *
  • Posts: 71
  • Country: np
A crucial requirement for demand paging is the ability to restart any instruction after a page fault.
...
...
...
Quote
The major difficulty arises when one instruction may modify several different locations. For example, consider the IBM system 360/370 MVC(Move character) instruction, which can move up to 256 bytes from one location to another(possibly overlapping) location. If either block(source or destination) straddles a page boundary, a page fault might occur after the move is partially done. In addition, if the source and destination blocks overlap, the source block may have been modified, in which case we cannot simply restart the instruction.
...
...
...

I did not quite get to visualize what is that coded text trying to potray.

Here is some information about the MVC instruction of IBM

https://punctiliousprogrammer.com/wp-content/uploads/2022/09/MVC.pdf



Quote
MVC is an instruction which is designed to copy a collection of consecutive bytes from one storage location to another


Quote
The copying operation is usually straightforward, but can be more complicated by overlapping  the source and target fields. Keep in mind that the copy is made one byte at a time. Consider the following examples,




It somewhat covers that aspect.

But even after all the research, I cannot be entirely plaused with this information. Thus I seek help.
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 4832
  • Country: us
Re: When does restarting instruction becomes difficult after page fault?
« Reply #1 on: October 28, 2025, 11:48:55 am »
On many processors, most instruction will either complete or do nothing at all.  This makes it easy for a page fault handler to simply restart the trapped instruction.

Some instructions can fault part way through. This is typical of instructions that store multiple memory locations, but there can be other reasons.

In that case one of theee things needs to happen.  Either you restart the instruction from the beginning, you resume it from where it left off, or you emulate the rest instruction in the trap handler and resume to the next instruction.

The easiest thing to do is restart from the beginning.  But that only works if repeating the instruction has the same behavior.  This is easily true for instructions that store multiple registers to memory.  The repeated store will overwrite the data with the same value.  But for a memcpy instruction if the buffers overlap the "source" memory could have been modified.  So you can't just restart or you will get the wrong result.

The next option is to resume the instruction.  This works if the state is stored in a visible register.  For instance the x86 repmovsb instruction is a single instruction that does memcpy but it updates the source and destination pointed and loop counter registers as it goes.  So resuming it works fine.

The remaining option would be to emulate the instruction.  In that case you need to decode the instruction that triggered the fault, check the memory address that faulted, and use that to figure out how far it got before faulting.  Then you copy the remainder of the data, increment the stored program counter, and return to the next instruction.
 

Offline 0xdeadbeef

  • Super Contributor
  • ***
  • Posts: 1894
  • Country: de
Re: When does restarting instruction becomes difficult after page fault?
« Reply #2 on: October 28, 2025, 11:56:56 am »
Just my 0,02€: MPU/MMU traps and the like are not necessarily causing a trap immediately on every architecture.
I worked with architectures where such a trap occurred a few instruction later, partly dependent on other factors (e.g. read or write) and due to pipeline effects and what not, not even the processor manufacturer would give a 100% safe statement how many instructions later the trap could occur.
This makes it somewhat difficult/dangerous to jump back to the instruction causing the trap (or the next instruction after that) since the register containing the program counter (PC) at which the trap vector was entered to is not necessarily directly related to the instruction causing the trap.
Trying is the first step towards failure - Homer J. Simpson
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 190
  • Country: us
Re: When does restarting instruction becomes difficult after page fault?
« Reply #3 on: October 28, 2025, 04:01:19 pm »
I did not quite get to visualize what is that coded text trying to potray.

It is simply telling you that if you are copying between non-overloapping regions of memory, then abandoning the copying at any moment and then restarting it from the very beginning is easy: just restart it and no one will notice. Done.

But if you are copying overlapping regions, sometime during the copying the instruction might override some of the data in the source region. In which case restarting this instruction from the beginning is not possible: the source data has already been lost.

Here's an example:

Let's say you have a byte sequence in memory '****[0123456789]****' (square brackets mark the source region). And, say, you are copying it 2 bytes to the left. The proper result for left-to-right copying instruction is '**01[2345678989]****'. Page fault or no page fault, you are expecting to get this result.

However, let's say page fault happened when the instruction was reading byte '6' from memory. At that moment the memory state was '**01[2345456789]****'. As you can see, we are in the middle of the copying, and the initial portion of the original data has already been lost/overwritten. You cannot just nonchalantly restart the instruction from the beginning, since the result will be incorrect.

That's exactly what your quote is talking about.
« Last Edit: October 28, 2025, 05:20:28 pm by TheCalligrapher »
 
The following users thanked this post: shivajikobardan

Offline helius

  • Super Contributor
  • ***
  • Posts: 3748
  • Country: us
Re: When does restarting instruction becomes difficult after page fault?
« Reply #4 on: October 28, 2025, 04:57:37 pm »
Several computer architectures that have complex, multi-part instructions (like block moves) deal with restartability by means of a "first part done flag". Before the instruction is entered, the flag is set to false. Before copying anything, the instruction places a count word on the stack and sets the flag to true. Now the instruction can keep the number of words copied in the count, and can be interrupted in the middle of a copy, then restarted from where it left off.
A perceptive reader will notice that the special flag is not strictly necessary, and the instruction could simply require the count word (as zero) on the stack when it is executed.
 

Online peter-h

  • Super Contributor
  • ***
  • Posts: 5966
  • Country: gb
  • Doing electronics since the 1960s...
Re: When does restarting instruction becomes difficult after page fault?
« Reply #5 on: November 12, 2025, 02:13:24 pm »
This is somewhat relevant
https://www.eevblog.com/forum/microcontrollers/st-32f417-any-way-to-make-an-spi-sram-to-look-like-normal-ram/

If there was a general solution for say arm32 thumb, it would be great because one could map say an 8MB PSRAM into some unused address region, and the page fault handler would perform the SPI read or write.

BTW, the image in post 1 is not visible. I believe it is Imgur blocking display of their content to European IP addresses.
« Last Edit: November 12, 2025, 04:11:17 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf