Line 125:
Line 125:
|<code>uint32_t OSGetCoreId(void);</code>
|<code>uint32_t OSGetCoreId(void);</code>
|Returns the PowerPC coreid this code is running on(value of spr1007).
|Returns the PowerPC coreid this code is running on(value of spr1007).
+
|}
+
+
===Codegen===
+
These functions are used for managing the codegen/JIT area which begins at virtual-memory address 0x01800000. The codegen area size, whether codegen can be used at all, and permissions/etc are loaded from cos.xml for the process. Hence, almost all processes which don't use WebKit don't have access to codegen. These permissions include the coreid which is allowed to use codegen at all(with [[Internet Browser]] this is core1). These permissions also seem to include whether the process is allowed to use OSCodegenCopy([[Internet Browser]] isn't allowed to use it).
+
+
{| class="wikitable"
+
!Name
+
!Prototype
+
!Description
+
|-
+
|OSGetCodegenVirtAddrRange
+
|<code>void OSGetCodegenVirtAddrRange(uint32_t *outaddr, uint32_t *outsize);</code>
+
|This calls syscall 0x4e00 then writes the output r3/r4 from the syscall, to outaddr/outsize. The outaddr value is codegen+0, outsize is the size of the entire codegen memory that's currently mapped. When codegen isn't available, the output values are all-zero.
+
|-
+
|OSGetCodegenCore
+
|<code>uint32_t OSGetCodegenCore(void);</code>
+
|This returns (syscall_7600() & 0x3), which is the coreid that's allowed to use codegen.
+
|-
+
|OSGetCodegenMode
+
|<code>uint32_t OSGetCodegenMode(void);</code>
+
|This returns (syscall_7600() & ~0x3), which contains some of the flags used by the kernel for codegen permission checking. The only bits which the kernel can ever set here are for bitmask 0xC0000000.
+
|-
+
|OSSwitchSecCodeGenMode
+
|<code>uint32_t OSSwitchSecCodeGenMode(bool flag);</code>
+
|This adjusts the entire codegen memory permissions, if codegen is available. 0: RW- permissions. Non-zero: R-X permissions. This returns 0 for failure, non-zero for success.
+
|-
+
|OSGetSecCodeGenMode
+
|<code>uint32_t OSGetSecCodeGenMode(void);</code>
+
|This just calls syscall 0x7700. Which then returns 1 when codegen is available+mapped, 0 otherwise.
+
|-
+
|OSCodegenCopy
+
|<code>uint32_t OSCodegenCopy(uint32_t *dstaddr, uint32_t *srcaddr, uint32_t size);</code>
+
|In coreinit this just executes syscall 0x7800. This returns 0 for failure, 1 for success. The dstaddr(+size) must be within the codegen memory(integer overflow with dstaddr+size is checked for). If all of the permissions/bounds checks pass(size must not be zero), the kernel will then set the codegen permissions to RW- with code used by SwitchSecCodeGenMode internally. Then it copies the input data from userland memory to the dstaddr with the specified size. Lastly, the codegen permissions are set to R-X with the previously mentioned internal function.
|}
|}