Skip to contentSkip to conversation

Dynamically altering code segments

DISCUSSION Jeremy Jackson 1 participant 1 message Mar 1, 2001

lore
Share & tools
lore
#0 lore raw
Quoted 3 lines · Tom

Hi...

This is my first post, so if this is off topic for this list, please

direct

Quoted 4 lines

me
to another one that is more appropriate. Thanks

That said, I am wanting to dynamically modify the kernel in specific

places

Quoted 2 lines

to
implement a custom kernel trace mechanism. The general idea is that,

when

Quoted 2 lines

the
"trace" is off, there are NOP instruction sequences at various places

in the

Quoted 1 line

kernel. When the "trace" is turned on, those same NOPs are replaced

by JMPs

Quoted 1 line

to code that implements the trace (such as logging events, using the

MSR and

Quoted 3 lines

PMC's etc..).

This was a trick that was done in my old days of OS/2 performance

tools

Quoted 1 line

developement to get trace information from the running kernel. In

that

Quoted 2 lines

case,
we simply remapped the appropriate code segments to data segments (I

think

Quoted 1 line

back then it was called 'aliasing code segments') and used that

segment to

Quoted 4 lines

make changes to the kernel code on the fly.

Is it possible to do the same thing in Linux?

the CS and DS segment descriptors already both map 0-4G, the DS being read-write.
what you want is to change page protections, the system call mprotect() comes
to mind.