SlideShare a Scribd company logo
How to get LBR contents
on Intel x86
Reading Last Branch Record MSRs using a
simple Linux kernel module
M.Golyani
(MAGMAG)
Table of contents
● What is LBR?
● What is a branch
instruction?
● What is MSR?
● Accessing LBR
● A little about rings
● Enabling LBR in Intel
● WRMSR, RDMSR
● Filtering LBR
● Address of LBR registers
● Reading LBR
● One MSR set for each CPU
● Entering ring-0
● LKM 4 LBR
What is LBR
● Intel says:
“Processors based on Intel micro-architecture (Nehalem) provide 16
pairs of MSR to
record last branch record information.”
● Nehalem??
Intel uses code names for it's products. Nehalem is the codename of Intel
micro-architecture. First CPU using this arch was core i7, released in 2008.
What is Branch
● From Wikipedia:
“A branch is an instruction in a computer program that may, when
executed by a computer, cause the computer to begin execution of a
different instruction sequence.”
● Instructions like: jmp, call, jz, jnz, …. are all branch instructions.
● When a branch instruction is executed, the execution flow, redirects from
where it was to a specific destination.
● Here, the term “Source” is the address where this instruction is located and
the term “Destination” is the address where it is redirecting to.
What is MSR
●
Wikipedia says:
“A model-specific register (MSR) is any of various control registers in the x86 instruction
set used for debugging, program execution tracing, computer performance monitoring,
and toggling certain CPU features”
● Intel says:
- “Most IA-32 processors (starting from Pentium processors) and Intel 64 processors
contain a model-specific registers (MSRs). A given MSR may not be supported across
all families and models for Intel 64 and IA-32 processors.
- Some MSRs are designated as architectural to simplify software programming; a
feature introduced by an architectural MSR is expected to be supported in future
processors. Non-architectural MSRs are not guaranteed to be supported or to have the
same functions on future processors.”
MSR_LASTBRANCH_1_FROM_IP
MSR_LASTBRANCH_14_FROM_IP
MSR_LASTBRANCH_15_FROM_IP
MSR_LASTBRANCH_0_FROM_IP
MSR_LASTBRANCH_1_TO_IP
MSR_LASTBRANCH_14_TO_IP
MSR_LASTBRANCH_15_TO_IP
MSR_LASTBRANCH_0_TO_IP
When LBR is enabled in a processor, the source address of
latest executed branch instructions is stored in one of
MSR_LASTBRANCH_#_FROM_IP registers and the destination resides in
equivalent MSR_LASTBRANCH_#_TO_IP register
Accessing LBR
● To access LBR in a processor, we should first enable this option
in desired processor.
● After enabling LBR, we can use Intel's “rdmsr” instruction to
read the contents of LBR model specific registers.
● Each MSR, has a number (Address) in every processor and to
access a LBR, we should use that address with rdmsr
instruction.
● The rdmsr instruction must be executed in ring 0 (kernel mode)
Kernel, the lord of the rings
● Wikipedia:
In computer science, hierarchical protection domains, often called protection rings, are
mechanisms to protect data and functionality from faults (by improving fault tolerance)
and malicious behavior (by providing computer security).
● Me!!:
Protection rings is an access control mechanism used in some
operating systems (Multics,...) and is implemented in some processors.
Read “operating system security” (Trent Jaeger) for further information.
Kernel, the lord of the rings
Ash nazg durbatulûk ,
Ash nazg gimbatul,
Ash nazg thrakatulûk
Agh burzum­ishi 
krimpatul.
In Linux, kernel modules run here
(image from wikipedia)
Enabling LBR
● To enable LBR, you should read Intel's data-sheet of your system's
processor (if it's Intel).
● In “Intel® 64 and IA-32 Architectures Software Developer’s Manual”, it
is mentioned that enabling LBR is done using a MSR with address of
01D9H.
● Take care in reading these data-sheets, the MSR addresses may vary
across different processors of Intel (although, usually are the same).
● My processor is an Intel core i7 (/proc/cpuinfo), so I used the
information listed in section 19.6 of this data-sheet.
Enabling LBR
The first bit of IA32_DEBUGCTL MSR should be set to 1 for enabling LBR
in each of my CPUs (Intel core i7 on lenovo thinkpad T420)
WRMSR,RDMSR
● To change a MSR value, we should use “wrmsr” instruction and to
read a MSR value, “rdmsr” is used.
● wrmsr and rdmsr must be executed in ring-0.
● Reading Intel instruction set reference, will teach us how to use these
instructions.
Enabling LBR
● Finding that IA32_DEBUGCTL MSR is located at address 1D9H, we
can use the following code to set it's first bit to “1” and rest of them to
“0” :
asm volatile    (
                        "xor %%edx, %%edx;"
                        "xor %%eax, %%eax;"
                        "inc %%eax;"
                        "mov $0x1d9, %%ecx;"
                        "wrmsr;"
                        :
                        :
                        :
                        );
Filtering LBR
● After enabling LBR, we can filter it to contain only user-space branch
traces.
According to appendix B, section B.4 in Intel software developer's manual,
MSR_LBR_SELECT for Nehalem based CPUs is located at 1C8H.
Filtering LBR
● To filter LBR to contain only user-space branches, it's enough to write
“0x1” into MSR_LBR_SELECT register (located at 1C8H).
asm volatile    (
                        "xor %%edx, %%edx;"
                        "xor %%eax, %%eax;"
                        "inc %%eax;"
                        "mov $0x1c8, %%ecx;"
                        "wrmsr;"
                        :
                        :
                        :
                        );
Address of LBR registers
● The 16 MSR pairs which contain last branch record, for my CPU is
located at 680H (1664 D) up to 68FH regarding to 16 registers of
MSR_LASTBRANCH_FROM_IP and from 6C0H to 6CFH regarding
to 16 registers of MSR_LASTBRANCH_TO_IP.
● Each FROM_IP MSR, indicates the “source” of branch and
corresponding TO_IP MSR, indicates the “destination” of that branch.
● Table B-5, MSRs in Processors Based on Intel Microarchitecture is for
my CPU. Find Yours yourself :D
Reading LBR
  int ax1f,dx1f,ax1t,dx1t,msr_from_counter1,msr_to_counter1;
  for(msr_from_counter1=1664,msr_to_counter1=1728;msr_from_counter1<1680;msr_from_counter1++,
msr_to_counter1++)  
{
    asm volatile    (
                    "mov %4, %%ecx;"
                    "rdmsr;"
                    "mov %%eax, %0;"
                    "mov %%edx, %1;"
                    "mov %5, %%ecx;"
                    "rdmsr;"
                    "mov %%eax, %2;"
                    "mov %%edx, %3;"
                    : "=g" (ax1f), "=g" (dx1f), "=g" (ax1t), "=g" (dx1t)
                    : "g" (msr_from_counter1), "g" (msr_to_counter1)
                    : "%eax", "%ecx", "%edx"
                    );
   printk(KERN_INFO "On cpu %d, branch from: %8x (MSR: %X), to %8x (MSR: %X)n",
smp_processor_id(),ax1f,msr_from_counter1,ax1t,msr_to_counter1);
 }
● To read LBR, you can use a “for” loop in conjunction with printk() as
follow:
One MSR set for each CPU
● Each CPU has it's own MSR registers, so it's very possible that you
enable LBR on one CPU and have it disabled on others.
● This could lead to lost of branch traces as the target application, will
probably run on all of existing CPUs (unless using processor affinity).
● To enable LBR on all CPUs, best way (AFAIK) is to write a multi-thread
code with number of threads equal to number of processors, then
binding each thread to one processor and finally enabling LBR on each
of them.
Entering ring-0
● As mentioned before, wrmsr and rdmsri must be executed in ring-0.
● To do so, the simplest way (again AFAIK) is to write a kernel module
and then inserting it into kernel (insmod).
● Using a KM, we can print LBR contents on /var/log/kern.log
(/var/log/messages) using “printk()” function.
● A very good point to start writing kernel modules is “The Linux Kernel
Module Programming Guide” written by Peter Salzman et al. Although
there are some differences between writing a KM for kernel 2.X and
3.x.
LKM 4 LBR
● BTW, to compile a kernel module, first you should obtain the running
kernel source files (linux-headers) Here is mine:
LKM 4 LBR
● After that, you can start writing your code and creating appropriate
Makefile for it, like this:
LKM 4 LBR
● After creating Makefile, you can compile your module, using make
command:
LKM 4 LBR
● Here is my read_LBR.c:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/sched.h>
//#include <linux/delay.h>
#include <linux/smp.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("M.Golyani");
static struct task_struct * ts1;
static struct task_struct * ts2;
static struct task_struct * ts3;
static struct task_struct * ts4;
int thread_core_1(void)
{
        int ax1f,dx1f,ax1t,dx1t,msr_from_counter1,msr_to_counter1;
//      msleep(50000);
// enable LBR:
        asm volatile    (
                        "xor %%edx, %%edx;"
                        "xor %%eax, %%eax;"
                        "inc %%eax;"
                        "mov $0x1d9, %%ecx;"
                        "wrmsr;"
                        :
                        :
                        );
//      printk(KERN_INFO "LBR Enabled on core 1...n");
// Filter LBR to only contain user space branches.
        asm volatile    (
                        "xor %%edx, %%edx;"
                        "xor %%eax, %%eax;"
                        "inc %%eax;"
                        "mov $0x1c8, %%ecx;"
                        "wrmsr;"
                        :
                        :
                        :
                        );
        for(msr_from_counter1=1664,msr_to_counter1=1728;msr_from_counter1<1680;msr_from_counter1++,msr_to_counter1++)  
        {
                asm volatile    (
                        "mov %4, %%ecx;"
                        "rdmsr;"
                        "mov %%eax, %0;"
                        "mov %%edx, %1;"
                        "mov %5, %%ecx;"
                        "rdmsr;"
                        "mov %%eax, %2;"
                        "mov %%edx, %3;"
                        : "=g" (ax1f), "=g" (dx1f), "=g" (ax1t), "=g" (dx1t)
                        : "g" (msr_from_counter1), "g" (msr_to_counter1)
                        : "%eax", "%ecx", "%edx"
                        );
                printk(KERN_INFO "In thread 1 on cpu %d, branch from: %8x (MSR: %X), to %8x (MSR: %X)n",
smp_processor_id(),ax1f,msr_from_counter1,ax1t,msr_to_counter1);
        }
        if (kthread_should_stop())
        {
                printk(KERN_INFO "STOP1n");
                return 0;
        }
        do_exit(0);
}
// Other threads are same as first one, just rename variables appropriately. Simple copy­paste ;o)
// Here goes the init and exit function of our module:
int __init start_function(void)
{
        ts1=kthread_create(thread_core_1,NULL,"KTH1");
        kthread_bind(ts1,0);
        ts2=kthread_create(thread_core_2,NULL,"KTH2");
        kthread_bind(ts2,1);
        ts3=kthread_create(thread_core_3,NULL,"KTH3");
        kthread_bind(ts3,2);
        ts4=kthread_create(thread_core_4,NULL,"KTH4");
        kthread_bind(ts4,3);
        if (!IS_ERR(ts1) && !IS_ERR(ts2) && !IS_ERR(ts3) && !IS_ERR(ts4))
        {
                wake_up_process(ts1);
                wake_up_process(ts2);
                wake_up_process(ts3);
                wake_up_process(ts4);
        }
        else
        {
                printk(KERN_INFO "Failed to bind thread to CPUn");
        }
        return 0;
}
void __exit end_function(void)
{
        printk(KERN_INFO "Bye bye...n");
}
module_init(start_function);
module_exit(end_function);
‫باشد‬ ‫همین‬ ‫و‬ ‫گفتیم‬ ‫معنی‬ ‫این‬ ‫از‬ ‫نکته‬ ‫یک‬

More Related Content

PDF
MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...
Jean-François Gagné
 
PDF
The Spectre of Meltdowns
Andriy Berestovskyy
 
PDF
InnoDB Flushing and Checkpoints
MIJIN AN
 
PDF
LLVM Register Allocation (2nd Version)
Wang Hsiangkai
 
PDF
InnoDB MVCC Architecture (by 권건우)
I Goo Lee.
 
PDF
MySQL Buffer Management
MIJIN AN
 
PDF
Boost.Preprocessorでプログラミングしましょう
digitalghost
 
PDF
How queries work with sharding
MongoDB
 
MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...
Jean-François Gagné
 
The Spectre of Meltdowns
Andriy Berestovskyy
 
InnoDB Flushing and Checkpoints
MIJIN AN
 
LLVM Register Allocation (2nd Version)
Wang Hsiangkai
 
InnoDB MVCC Architecture (by 권건우)
I Goo Lee.
 
MySQL Buffer Management
MIJIN AN
 
Boost.Preprocessorでプログラミングしましょう
digitalghost
 
How queries work with sharding
MongoDB
 

What's hot (20)

PDF
semaphore & mutex.pdf
Adrian Huang
 
PDF
New Ways to Find Latency in Linux Using Tracing
ScyllaDB
 
PDF
プログラミングコンテストでのデータ構造
Takuya Akiba
 
PDF
The MySQL Query Optimizer Explained Through Optimizer Trace
oysteing
 
PDF
文字列検索のいろいろ
Kazuma Mikami
 
PDF
The Linux Block Layer - Built for Fast Storage
Kernel TLV
 
PDF
Part II: LLVM Intermediate Representation
Wei-Ren Chen
 
PDF
x86とコンテキストスイッチ
Masami Ichikawa
 
PDF
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
Anne Nicolas
 
PPTX
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
Adrian Huang
 
PDF
Advanced MySQL Query Tuning
Alexander Rubin
 
PDF
InnoDB Locking Explained with Stick Figures
Karwin Software Solutions LLC
 
PPT
Glibc malloc internal
Motohiro KOSAKI
 
PDF
プログラミングコンテストでの乱択アルゴリズム
Takuya Akiba
 
PDF
Linux Profiling at Netflix
Brendan Gregg
 
PDF
Reverse Mapping (rmap) in Linux Kernel
Adrian Huang
 
PDF
SEH overwrite and its exploitability
FFRI, Inc.
 
PDF
Qemu Introduction
Chiawei Wang
 
PDF
プログラミングコンテストでのデータ構造 2 ~動的木編~
Takuya Akiba
 
PPTX
SQLチューニング入門 入門編
Miki Shimogai
 
semaphore & mutex.pdf
Adrian Huang
 
New Ways to Find Latency in Linux Using Tracing
ScyllaDB
 
プログラミングコンテストでのデータ構造
Takuya Akiba
 
The MySQL Query Optimizer Explained Through Optimizer Trace
oysteing
 
文字列検索のいろいろ
Kazuma Mikami
 
The Linux Block Layer - Built for Fast Storage
Kernel TLV
 
Part II: LLVM Intermediate Representation
Wei-Ren Chen
 
x86とコンテキストスイッチ
Masami Ichikawa
 
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
Anne Nicolas
 
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
Adrian Huang
 
Advanced MySQL Query Tuning
Alexander Rubin
 
InnoDB Locking Explained with Stick Figures
Karwin Software Solutions LLC
 
Glibc malloc internal
Motohiro KOSAKI
 
プログラミングコンテストでの乱択アルゴリズム
Takuya Akiba
 
Linux Profiling at Netflix
Brendan Gregg
 
Reverse Mapping (rmap) in Linux Kernel
Adrian Huang
 
SEH overwrite and its exploitability
FFRI, Inc.
 
Qemu Introduction
Chiawei Wang
 
プログラミングコンテストでのデータ構造 2 ~動的木編~
Takuya Akiba
 
SQLチューニング入門 入門編
Miki Shimogai
 
Ad

Similar to How to get LBR contents on Intel x86 (20)

PPTX
Processor types
Amr Aboelgood
 
PDF
ARM Architecture and Meltdown/Spectre
GlobalLogic Ukraine
 
PDF
GOD MODE UNLOCKED - Hardware Backdoors in x86 CPUs
Priyanka Aash
 
PPTX
ARM
Ahmed Mahmoud
 
PDF
Arm architecture overview
Sathish Arumugasamy
 
PPT
Arm teaching material
Nilesh Bhandare
 
PPT
Arm teaching material
John Williams
 
PPT
ARM Micro-controller
Ravikumar Tiwari
 
PPTX
Arm cortex-m4 programmer model
Mohammed Gomaa
 
PPSX
Lect 3 ARM PROCESSOR ARCHITECTURE
Dr.YNM
 
PPTX
Lecture9
misgina Mengesha
 
PPT
ARMicrocontroller Memory and Exceptions,Traps.ppt
ECEHITS
 
DOC
Linux interrupts
mukul bhardwaj
 
PDF
2 introduction to arm architecture
satish1jisatishji
 
PDF
semester 6_arm processor basics Mod 3_part 1.pdf
lekhapankaj1
 
PPTX
Introduction to ARM Systems-11-17-2012.pptx
mithunkarthikb24
 
PDF
Exploiting arm linux
Dan H
 
PPT
LPC 2148 Instructions Set.ppt
ProfBadariNathK
 
PPTX
GCC for ARMv8 Aarch64
Yi-Hsiu Hsu
 
PPTX
3.TechieNest microcontrollers
TechieNest Pvt. Ltd .
 
Processor types
Amr Aboelgood
 
ARM Architecture and Meltdown/Spectre
GlobalLogic Ukraine
 
GOD MODE UNLOCKED - Hardware Backdoors in x86 CPUs
Priyanka Aash
 
Arm architecture overview
Sathish Arumugasamy
 
Arm teaching material
Nilesh Bhandare
 
Arm teaching material
John Williams
 
ARM Micro-controller
Ravikumar Tiwari
 
Arm cortex-m4 programmer model
Mohammed Gomaa
 
Lect 3 ARM PROCESSOR ARCHITECTURE
Dr.YNM
 
ARMicrocontroller Memory and Exceptions,Traps.ppt
ECEHITS
 
Linux interrupts
mukul bhardwaj
 
2 introduction to arm architecture
satish1jisatishji
 
semester 6_arm processor basics Mod 3_part 1.pdf
lekhapankaj1
 
Introduction to ARM Systems-11-17-2012.pptx
mithunkarthikb24
 
Exploiting arm linux
Dan H
 
LPC 2148 Instructions Set.ppt
ProfBadariNathK
 
GCC for ARMv8 Aarch64
Yi-Hsiu Hsu
 
3.TechieNest microcontrollers
TechieNest Pvt. Ltd .
 
Ad

More from Mohammad Golyani (8)

PDF
A holistic Control Flow Integrity
Mohammad Golyani
 
PPTX
GCC, Glibc protections
Mohammad Golyani
 
PPTX
GCC, Glibc protections
Mohammad Golyani
 
PPTX
Exec-shield
Mohammad Golyani
 
PPT
Advanced c programming in Linux
Mohammad Golyani
 
PPTX
Data encryption standard
Mohammad Golyani
 
PDF
Linux Protections Against Exploits
Mohammad Golyani
 
A holistic Control Flow Integrity
Mohammad Golyani
 
GCC, Glibc protections
Mohammad Golyani
 
GCC, Glibc protections
Mohammad Golyani
 
Exec-shield
Mohammad Golyani
 
Advanced c programming in Linux
Mohammad Golyani
 
Data encryption standard
Mohammad Golyani
 
Linux Protections Against Exploits
Mohammad Golyani
 

Recently uploaded (20)

PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 

How to get LBR contents on Intel x86