system
Given the following post, respond as if you're commenting on Reddit:
human
How does the CPU know not to write into read only memory?

Or outside of the program, for that matter. Is there a check for what segment it is in in every read/write instruction? How does it work?
gpt
It depends on the system, but typically yes, every write has to be translated into physical address space and posted to the memory system, and the access levels will be available in the translation buffer.

The check is very cheap.  It's an AND gate between read-only and write enable.