Merge "makefile : Change to add MEMRWOFF for targets this is not declared."
diff --git a/arch/arm/include/arch/arm/mmu.h b/arch/arm/include/arch/arm/mmu.h
index 43a474e..c870329 100644
--- a/arch/arm/include/arch/arm/mmu.h
+++ b/arch/arm/include/arch/arm/mmu.h
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2008 Travis Geiselbrecht
  *
- * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2015,2018 The Linux Foundation. All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files
@@ -49,6 +49,7 @@
 #define MMU_MEMORY_AP_NO_ACCESS     (0x0 << 10)
 #define MMU_MEMORY_AP_READ_ONLY     (0x7 << 10)
 #define MMU_MEMORY_AP_READ_WRITE    (0x3 << 10)
+#define MMU_MEMORY_APX_READ_ONLY    (0x1 << 15)
 
 #define MMU_MEMORY_XN               (0x1 << 4)
 #else /* LPAE */
diff --git a/arch/arm/rules.mk b/arch/arm/rules.mk
index 7e3ace0..bfef682 100644
--- a/arch/arm/rules.mk
+++ b/arch/arm/rules.mk
@@ -150,7 +150,7 @@
 $(BUILDDIR)/system-onesegment.ld: $(LOCAL_DIR)/system-onesegment.ld $(LK_TOP_DIR)/target/$(TARGET)/rules.mk .FORCE
 	@echo generating $@
 	@$(MKDIR)
-	$(NOECHO)sed "s/%MEMBASE%/$(MEMBASE)/;s/%MEMSIZE%/$(MEMSIZE)/" < $< > $@
+	$(NOECHO)sed "s/%MEMBASE%/$(MEMBASE)/;s/%MEMSIZE%/$(MEMSIZE)/;s/%MEMRWOFF%/$(MEMRWOFF)/" < $< > $@
 
 $(BUILDDIR)/system-twosegment.ld: $(LOCAL_DIR)/system-twosegment.ld $(LK_TOP_DIR)/target/$(TARGET)/rules.mk .FORCE
 	@echo generating $@
diff --git a/arch/arm/system-onesegment.ld b/arch/arm/system-onesegment.ld
index f2c62ac..7768853 100644
--- a/arch/arm/system-onesegment.ld
+++ b/arch/arm/system-onesegment.ld
@@ -37,6 +37,9 @@
 	.init : { *(.init) } =0x9090
 	.plt : { *(.plt) }
 
+
+	/* Use read/write memory offset for everything except code section */
+	. = %MEMRWOFF% ;
 	.rodata : { 
 		*(.rodata .rodata.* .gnu.linkonce.r.*)
 		. = ALIGN(4);
diff --git a/makefile b/makefile
index 417b414..b2d40a6 100644
--- a/makefile
+++ b/makefile
@@ -211,6 +211,13 @@
 	ARCH_$(ARCH)=1 \
 	$(addsuffix =1,$(addprefix WITH_,$(ALLMODULES)))
 
+# Add MEMRWOFF as . for targets this is not declared.
+# . will be replaced as string in linker file.
+ifeq ($(MEMRWOFF),)
+MEMRWOFF:= .
+DEFINES += MEMRWOFF=$(MEMRWOFF)
+endif
+
 # debug build?
 ifneq ($(DEBUG),)
 DEFINES += \
diff --git a/platform/msm8953/platform.c b/platform/msm8953/platform.c
index 5f75b70..87d45f3 100644
--- a/platform/msm8953/platform.c
+++ b/platform/msm8953/platform.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -45,9 +45,17 @@
 #define APPS_SS_SIZE   ((APPS_SS_END - APPS_SS_BASE)/MB)
 
 /* LK memory - cacheable, write through */
+#ifdef SECURE_CODE_MEM
+#define LK_MEMORY         (MMU_MEMORY_TYPE_NORMAL_WRITE_BACK_ALLOCATE | \
+				MMU_MEMORY_AP_READ_ONLY |\
+				MMU_MEMORY_APX_READ_ONLY)
+
+#define LK_MEMORY_RW      (MMU_MEMORY_TYPE_NORMAL_WRITE_BACK_ALLOCATE | \
+				MMU_MEMORY_AP_READ_WRITE | MMU_MEMORY_XN)
+#else
 #define LK_MEMORY         (MMU_MEMORY_TYPE_NORMAL_WRITE_BACK_ALLOCATE | \
 				MMU_MEMORY_AP_READ_WRITE)
-
+#endif
 /* Peripherals - non-shared device */
 #define IOMAP_MEMORY      (MMU_MEMORY_TYPE_DEVICE_SHARED | \
 				MMU_MEMORY_AP_READ_WRITE | MMU_MEMORY_XN)
@@ -61,7 +69,12 @@
 
 static mmu_section_t mmu_section_table[] = {
 /*           Physical addr,         Virtual addr,            Size (in MB),     Flags */
+#ifdef SECURE_CODE_MEM
+	{    MEMBASE,               MEMBASE,                 1,                  LK_MEMORY},
+	{    MEMRWOFF,              MEMRWOFF,                (MEMSIZE / MB) - 1, LK_MEMORY_RW},
+#else
 	{    MEMBASE,               MEMBASE,                 (MEMSIZE / MB),   LK_MEMORY},
+#endif
 	{    MSM_IOMAP_BASE,        MSM_IOMAP_BASE,          MSM_IOMAP_SIZE,   IOMAP_MEMORY},
 	{    APPS_SS_BASE,          APPS_SS_BASE,            APPS_SS_SIZE,      IOMAP_MEMORY},
 	{    MSM_SHARED_IMEM_BASE,  MSM_SHARED_IMEM_BASE,    1,                COMMON_MEMORY},
diff --git a/project/msm8953.mk b/project/msm8953.mk
index 0c21ce0..4a502c6 100644
--- a/project/msm8953.mk
+++ b/project/msm8953.mk
@@ -43,6 +43,9 @@
 ENABLE_SMD_SUPPORT := 1
 ENABLE_PWM_SUPPORT := true
 
+#Comment this to disable this feature.
+DEFINES += SECURE_CODE_MEM=1
+
 #DEFINES += WITH_DEBUG_DCC=1
 DEFINES += WITH_DEBUG_LOG_BUF=1
 DEFINES += WITH_DEBUG_UART=1
diff --git a/target/msm8953/rules.mk b/target/msm8953/rules.mk
index 55aeb86..032988e 100644
--- a/target/msm8953/rules.mk
+++ b/target/msm8953/rules.mk
@@ -11,6 +11,11 @@
 MEMBASE := 0x8F600000 # SDRAM
 MEMSIZE := 0x00A00000 # 10MB
 
+ifneq (,$(findstring SECURE_CODE_MEM,$(DEFINES)))
+MEMRWOFF:= 0x8F700000 # MEMBASE + 1MB(section page size)
+DEFINES += MEMRWOFF=$(MEMRWOFF)
+endif
+
 BASE_ADDR        := 0x80000000
 SCRATCH_ADDR     := 0xA0100000
 SCRATCH_SIZE     := 510 # 510MB