zephyriot / zep-jira14

0 stars 0 forks source link

Mpu stack guard is not set when reaching main #2118

Open nashif opened 7 years ago

nashif commented 7 years ago

Reported by michel jaouen:

When main function is scheduled at start up , main stack overflow is not protected by mpu stack guard feature. mpu stack guard is set on main stack when main is re-scheduled.

It can be detected as follow : add the following line in .gdbinit

define smpu
printf "mpu activé %d\n",*(int*)0xE000ED94 &1
end

define mpu_attr
set $ap=($arg0>>24 & 0x7)
set $x=($arg0>>28 & 1)
set $taille = ($arg0 >> 1)& 0x1f
#printf "taille %x ", $taille
set $taille = 1 << ($taille+1)
#printf "taille %x ", $taille

set $debut = $arg1 & ~$taille
set $fin = $debut+$taille

set $sub = ($arg0 >> 8) & 0xf
if ($x==1) 
    printf "X "
    end
if ($ap==0)
    printf "P_NA_U_NA"
    end
if ($ap==1) 
    printf "P_RW_U_NA"
    end
if ($ap==2) 
    printf "P_RW_U_NA"
    end
if ($ap==3) 
    printf "P_RW_U_RW"
    end
if ($ap==4) 
    printf "P_??_U_??"
    end
if ($ap==5) 
    printf "P_RO_U_NA"
    end
if ($ap==6) 
    printf "P_RO_U_RO"
    end
if ($ap==7) 
    printf "P_RO_U_RO"
    end
printf "(%x %x , subreg %x)",$debut,$fin,$sub
end
define read_mpu 
set *((int *)0xE000ED98)=$arg0
set $rbar = *(int*)0xE000ED9C
set $rasr = *(int*)0xE000EDa0
printf "%d rasr=%x rbar=%x ", $arg0, $rbar,$rasr
if ($rasr & 1)
    mpu_attr $rasr $rbar&~0xf
    end
printf "\n"
end

define rmpu
smpu
set $t=0
while ($t<8)
    read_mpu $t
    set $t=$t+1
end
end
define smpu
printf "mpu activé %d\n",*(int*)0xE000ED94 &1
end

Attach a gdb and (gdb) br main (gdb) c Continuing. Info : halted: PC: 0x0800228a

Breakpoint 1, main () at /local/home/frq08711/zeyphir/mcu/zephyr/samples/mpu_stack_guard_test/src/main.c:66 66
(gdb) rmpu mpu activé 1 0 rasr=8000000 rbar=7080025 P_RO_U_RO(8000000 8080000 , subreg 0) 1 rasr=20000001 rbar=1308001f X P_RW_U_RW(20000000 20010000 , subreg 0) 2 rasr=20010002 rbar=1308001d X P_RW_U_RW(20010000 20018000 , subreg 0) 3 rasr=40000003 rbar=3100039 P_RW_U_RW(40000000 60000000 , subreg 0) 4 rasr=e0000004 rbar=3040037 P_RW_U_RW(e0000000 f0000000 , subreg 0) 5 rasr=5 rbar=0 6 rasr=6 rbar=0 7 rasr=7 rbar=0 (gdb) mpu config 6 is not set , when mpu is active , it shows for instance (gdb) rmpu mpu activé 1 0 rasr=8000000 rbar=7080025 P_RO_U_RO(8000000 8080000 , subreg 0) 1 rasr=20000001 rbar=1308001f X P_RW_U_RW(20000000 20010000 , subreg 0) 2 rasr=20010002 rbar=1308001d X P_RW_U_RW(20010000 20018000 , subreg 0) 3 rasr=40000003 rbar=3100039 P_RW_U_RW(40000000 60000000 , subreg 0) 4 rasr=e0000004 rbar=3040037 P_RW_U_RW(e0000000 f0000000 , subreg 0) 5 rasr=5 rbar=0 6 rasr=20001286 rbar=16060009 X P_RO_U_RO(20001280 200012a0 , subreg 0) 7 rasr=7 rbar=0

(Imported from Jira ZEP-2283)

nashif commented 7 years ago

by Mark Linkmeyer:

Andy Gross , can you set the priority of this bug please so we know how important it is to fix in 1.9 or later? Thx.