Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-2909 refactored IO_START implementation, added load/store of real flags register

emteere99e385da6a95
diff --git a/Ghidra/Processors/Atmel/data/languages/atmega256.pspec b/Ghidra/Processors/Atmel/data/languages/atmega256.pspec
index dec8fbab1c..8fcaa0cc49 100644
--- a/Ghidra/Processors/Atmel/data/languages/atmega256.pspec
+++ b/Ghidra/Processors/Atmel/data/languages/atmega256.pspec
@@ -12,8 +12,8 @@
-->
<volatile outputop="write_volatile" inputop="read_volatile">
- <range space="mem" first="0x20" last="0x5b"/>
- <range space="mem" first="0x5f" last="0xff"/>
+ <range space="mem" first="0x20" last="0x5a"/>
+ <range space="mem" first="0x60" last="0xff"/>
</volatile>
<default_symbols>
@@ -137,7 +137,7 @@
<symbol name="EIND" address="mem:0x5c"/>
<symbol name="_SPL" address="mem:0x5d"/>
<symbol name="_SPH" address="mem:0x5e"/>
- <symbol name="SREG" address="mem:0x5f"/>
+
<symbol name="WDTCSR" address="mem:0x60"/>
<symbol name="CLKPR" address="mem:0x61"/>
diff --git a/Ghidra/Processors/Atmel/data/languages/avr8.pspec b/Ghidra/Processors/Atmel/data/languages/avr8.pspec
index 0fd33d0d3c..d878787d33 100644
--- a/Ghidra/Processors/Atmel/data/languages/avr8.pspec
+++ b/Ghidra/Processors/Atmel/data/languages/avr8.pspec
@@ -16,8 +16,8 @@
-->
<volatile outputop="write_volatile" inputop="read_volatile">
- <range space="mem" first="0x20" last="0x5b"/>
- <range space="mem" first="0x5f" last="0xff"/>
+ <range space="mem" first="0x20" last="0x5a"/>
+ <range space="mem" first="0x60" last="0xff"/>
</volatile>
<default_symbols>
@@ -42,7 +42,6 @@
<symbol name="TWI" address="code:0x11" entry="true"/>
<symbol name="SPM_RDY" address="code:0x12" entry="true"/>
- <symbol name="SREG" address="mem:0x5f"/>
<!-- See /usr/lib/avr/include/avr/iom64.h -->
<symbol name="PINF" address="mem:0x20"/>
diff --git a/Ghidra/Processors/Atmel/data/languages/avr8.sinc b/Ghidra/Processors/Atmel/data/languages/avr8.sinc
index 34a0876aff..3473574c8c 100644
--- a/Ghidra/Processors/Atmel/data/languages/avr8.sinc
+++ b/Ghidra/Processors/Atmel/data/languages/avr8.sinc
@@ -17,7 +17,6 @@ define alignment=2;
#define where the IO space is mapped if not specified
@ifndef IO_START
@define IO_START "0x20"
-@define RAMP_START "0x58"
@define EIND "0x5c"
@endif
@@ -33,12 +32,13 @@ define alignment=2;
# Ghidra, it's still preferable to see the name of IO locations used,
# rather than code addresses, so leave mem space as the default.
define space code type=ram_space size=$(PCBYTESIZE) wordsize=2 default;
-define space register type=ram_space size=2;
+define space register type=register_space size=2;
define space mem type=ram_space size=2 wordsize=1;
# Using decimal rather than hex to match specs
-# TODO: These general purpose registers should reside with the 'mem' space from 0x00-0x1f
-#define register offset=0 size=1 [
+# General registers start at 0 in the iospace for earlier avr8 processors
+# In the Xmega line, they are not accessible in mem, and are in a register space
+#
define $(REGISTER_SPACE) offset=0 size=1 [
R0 R1 R2 R3 R4 R5 R6 R7 R8 R9
R10 R11 R12 R13 R14 R15 R16 R17 R18 R19
@@ -46,7 +46,6 @@ define $(REGISTER_SPACE) offset=0 size=1 [
Zlo Zhi
];
-#define register offset=0 size=2 [
define $(REGISTER_SPACE) offset=0 size=2 [
R1R0 R3R2 R5R4 R7R6 R9R8
R11R10 R13R12 R15R14 R17R16 R19R18
@@ -54,7 +53,6 @@ define $(REGISTER_SPACE) offset=0 size=2 [
X Y Z
];
-#define register offset=0x10 size=4 [
define $(REGISTER_SPACE) offset=0x10 size=4 [
R19R18R17R16 R23R22R21R20
];
@@ -73,13 +71,30 @@ define register offset=0x80 size=1 [
#####################################
# Some AVR processors may have different io layouts not just different io.
-# This could necessitate modification to this section.
+#
# AVR processors with more than 64 KiB of RAM make use of the RAMP- registers
# to act as the high bits where the X, Y, or Z registers are used, or in direct
# addressing instructions.
-# TODO: Incorporate the RAMPD register in the LDS instruction.
-
-define mem offset=$(RAMP_START) size=1 [ RAMPD RAMPX RAMPY RAMPZ ];
+#
+# These IO registers need to be accessible to sleigh instruction PCODE
+# so they are defined here. The bulk of the IO registers are defined
+# as labels in the appropriate .pspec file.
+
+define mem offset=$(IO_START) size = 1 [
+# IO_START + 0x00
+ _ _ _ _ _ _ _ _
+ _ _ _ _ _ _ _ _
+# IO_START + 0x10
+ _ _ _ _ _ _ _ _
+ _ _ _ _ _ _ _ _
+
+# IO_START + 0x20
+ _ _ _ _ _ _ _ _
+ _ _ _ _ _ _ _ _
+# IO_START + 0x30
+ _ _ _ _ _ _ _ _
+ RAMPD RAMPX RAMPY RAMPZ _ _ _ SREG
+];
# If the AVR processor has more than 128 KiB of ROM, the processor will support the EIND
@@ -398,6 +413,23 @@ macro setMulFlags(res) {
$(Zflag) = (res == 0);
}
+macro loadSREG(reg) {
+ reg = (zext(Cflg==1) << 0) | (zext(Zflg==1) << 1) | (zext(Nflg==1) << 2) | (zext(Vflg==1) << 3) | (zext(Sflg==1) << 4) | (zext(Hflg==1) << 5) | (zext(Tflg==1)<<6) | (zext(Iflg==1) << 7);
+ SREG = reg;
+}
+
+macro storeSREG(val) {
+ Cflg = ((val>> 0) & 1);
+ Zflg = ((val>> 1) & 1);
+ Nflg = ((val>> 2) & 1);
+ Vflg = ((val>> 3) & 1);
+ Sflg = ((val>> 4) & 1);
+ Hflg = ((val>> 5) & 1);
+ Tflg = ((val>> 6) & 1);
+ Iflg = ((val>> 7) & 1);
+ SREG = val;
+}
+
# Handle possible skip instruction
# This next line is a NOP except for the phase, which is never really checked.
# A better fix may be to use -l, and ensure phase=1 is checked on the base constructors.
@@ -782,6 +814,9 @@ ElpmPlus: Z^"+" is Z {}
:in RdFull,SPH is phase=1 & ophi5=0x16 & RdFull & op9to10=3 & oplow4=0xe & SPH {
RdFull = SPH;
}
+:in RdFull,SREG is phase=1 & ophi5=0x16 & RdFull & op9to10=3 & oplow4=0xf & SREG {
+ loadSREG(RdFull);
+}
:inc RdFull is phase=1 & ophi7=0x4a & oplow4=0x3 & RdFull {
# inc doesn't set the C flag.
@@ -963,6 +998,9 @@ LpmPlus: Z^"+" is Z {}
:out SPH,RdFull is phase=1 & ophi5=0x17 & RdFull & op9to10=3 & oplow4=0xe & SPH {
SPH = RdFull;
}
+:out SREG,RdFull is phase=1 & ophi5=0x17 & RdFull & op9to10=3 & oplow4=0xf & SREG {
+ storeSREG(RdFull);
+}
:pop RdFull is phase=1 & ophi7=0x48 & oplow4=0xf & RdFull {
pop8(RdFull);
diff --git a/Ghidra/Processors/Atmel/data/languages/avr8xmega.pspec b/Ghidra/Processors/Atmel/data/languages/avr8xmega.pspec
index fa0edd4c6e..35b1d62315 100644
--- a/Ghidra/Processors/Atmel/data/languages/avr8xmega.pspec
+++ b/Ghidra/Processors/Atmel/data/languages/avr8xmega.pspec
@@ -12,8 +12,8 @@
-->
<volatile outputop="write_volatile" inputop="read_volatile">
- <range space="mem" first="0x00" last="0x3b"/>
- <range space="mem" first="0x3f" last="0xfff"/>
+ <range space="mem" first="0x00" last="0x37"/>
+ <range space="mem" first="0x40" last="0xfff"/>
</volatile>
<default_symbols>
@@ -1471,7 +1471,7 @@
<default_memory_blocks>
<memory_block name="iospace" start_address="mem:0x00" length="0x1000" initialized="false"/>
- <memory_block name="eeprom" start_address="mem:0x1000" length="0x1f00" initialized="false"/>
+ <memory_block name="eeprom" start_address="mem:0x1000" length="0x1000" initialized="false"/>
</default_memory_blocks>
diff --git a/Ghidra/Processors/Atmel/data/languages/avr8xmega.slaspec b/Ghidra/Processors/Atmel/data/languages/avr8xmega.slaspec
index 7eac477cbe..6fdb38bb03 100644
--- a/Ghidra/Processors/Atmel/data/languages/avr8xmega.slaspec
+++ b/Ghidra/Processors/Atmel/data/languages/avr8xmega.slaspec
@@ -5,7 +5,6 @@
@define IO_START "0"
@define REGISTER_SPACE "register"
-@define RAMP_START "0x38"
@define EIND "0x3c"
@include "avr8.sinc"