OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 12:31 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Cross-core RIP change on AMD64?
PostPosted: Wed Jan 24, 2018 8:25 pm 
Offline

Joined: Wed Jan 24, 2018 8:19 pm
Posts: 2
Poking at some weird things, figured this might be a good place to ask.

Suppose code running on one core, wanted to change RIP (longjmp) on another core.

Is there a direct way to do that? /proc/kallsyms on Linux has a bunch of interesting stuff in low memory, is anything like a memory map of registers? I can imagine poking interrupt vectors but I'm wondering if there's something not quite so passive.

Alternatively, suppose RIP was sitting on a HLT instruction in main memory. If I just rewrite that instruction does the processor just wake up and go, like a PAUSEing spinlock?

I dove into this goop fairly recently so I apologize if these are simple questions.


Top
 Profile  
 
 Post subject: Re: Cross-core RIP change on AMD64?
PostPosted: Wed Jan 24, 2018 8:40 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

dakami wrote:
Poking at some weird things, figured this might be a good place to ask.

Suppose code running on one core, wanted to change RIP (longjmp) on another core.

Is there a direct way to do that? /proc/kallsyms on Linux has a bunch of interesting stuff in low memory, is anything like a memory map of registers? I can imagine poking interrupt vectors but I'm wondering if there's something not quite so passive.


To do these things (on 80x86); you use the local APIC on one CPU to send an "inter-processor interrupt" (IPI) to the other CPU/s. The other CPUs receive the interrupt and start an interrupt hander, where the interrupt handler can do anything you like.

dakami wrote:
Alternatively, suppose RIP was sitting on a HLT instruction in main memory. If I just rewrite that instruction does the processor just wake up and go, like a PAUSEing spinlock?


Cross-modifying code requires synchronisation (otherwise the CPU can continue executing the old version of the code without realising it was changed). If one CPU is executing HLT and you replace the HLT instruction, then you'd need an interrupt to break out of the "already executing HLT", where the interrupt handler would serialise the instruction pipeline to make sure the CPU notices that the HLT was replaced with something else.


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: Cross-core RIP change on AMD64?
PostPosted: Thu Jan 25, 2018 1:19 am 
Offline

Joined: Wed Jan 24, 2018 8:19 pm
Posts: 2
OK this is starting to make a lot more sense.

What's the mechanism for talking to APICs? MMIO or...?

Any idea what latency on MONITOR/MWAIT is? Unlike HLT those *are* waiting for a main memory write.


Top
 Profile  
 
 Post subject: Re: Cross-core RIP change on AMD64?
PostPosted: Thu Jan 25, 2018 2:45 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

dakami wrote:
What's the mechanism for talking to APICs? MMIO or...?


There's the older xAPIC (which is limited to 256 APIC IDs); which is MMIO - each CPU's local APIC is found at the same physical address (a CPU can only talk to its own local APIC). Then there's the newer x2APIC (which can handle a lot more than 256 APIC IDs/CPUs), which uses MSRs instead.

dakami wrote:
Any idea what latency on MONITOR/MWAIT is? Unlike HLT those *are* waiting for a main memory write.


All things like this depend on the specific CPU and the specific situation, and for MWAIT itself it also depends on the hints you give it (mostly, which power saving state you tell the CPU to use while waiting). For a generic estimate (for the fastest power saving state only), if the write was done by a CPU within the same core I'd expect latency to be around 10 cycles, and if the write was done by a CPU in a completely different physical package that's multiple "NUMA hops" away I'd expect latency to be more like 100 cycles. For more aggressive power saving states this would increase by however long it takes the CPU to wake up from the power saving state.


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 223 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group