@@ -4935,6 +4935,9 @@ OS_RETURN_E cpuCopyVirtualCPUs(const kernel_thread_t* kpSrcThread,
49354935 pStackOffset ;
49364936 }
49374937
4938+ /* Update kernel stack end */
4939+ pCurVCpu -> kernelStackEnd = pDstThread -> kernelStackEnd - 0x10 ;
4940+
49384941 return OS_NO_ERR ;
49394942}
49404943
@@ -5425,10 +5428,8 @@ OS_RETURN_E cpuCopyLocalStorage(kernel_thread_t* pThread,
54255428 size_t align ;
54265429 size_t size ;
54275430 OS_RETURN_E error ;
5428- OS_RETURN_E intError ;
5429- uintptr_t tls ;
5430- uintptr_t tlsPhys ;
5431- void * pTmpData ;
5431+ uintptr_t srcTls ;
5432+ uintptr_t srcTlsPhys ;
54325433
54335434 if (pThread -> type == THREAD_TYPE_KERNEL )
54345435 {
@@ -5444,68 +5445,39 @@ OS_RETURN_E cpuCopyLocalStorage(kernel_thread_t* pThread,
54445445 return OS_NO_ERR ;
54455446 }
54465447
5447- /* Compute the memory size with alignement */
5448+
5449+ /* Get the original thread TLS virtual and physical addresses */
54485450 align = MAX (pThread -> pProcess -> mainTlsAlign , __alignof__(user_thread_t ));
5449- /* We do not support more than a page alignement */
5450- if (align > KERNEL_PAGE_SIZE )
5451- {
5452- return OS_ERR_NOT_SUPPORTED ;
5453- }
5451+ srcTls = (uintptr_t )kpSrcThread -> pUserThreadData -
5452+ ALIGN_UP (pThread -> pProcess -> mainTlsSize , align );
5453+ srcTlsPhys = memoryMgrGetPhysAddr (srcTls , kpSrcThread -> pProcess , NULL );
54545454
5455+ /* Copy the thread local storage that was COW */
54555456 size = ALIGN_UP (pThread -> pProcess -> mainTlsSize , align ) +
54565457 sizeof (user_thread_t );
54575458
54585459 /* Align to page size */
54595460 size = ALIGN_UP (size , KERNEL_PAGE_SIZE );
5460-
5461- pThread -> pUserThreadData = memoryUserAllocate (size ,
5462- MEMMGR_MAP_RW |
5463- MEMMGR_MAP_USER ,
5464- pThread -> pProcess ,
5465- & error );
5466- if (error != OS_NO_ERR )
5467- {
5468- return error ;
5469- }
5470-
5471- tlsPhys = memoryMgrGetPhysAddr ((uintptr_t )pThread -> pUserThreadData ,
5472- pThread -> pProcess ,
5473- NULL );
5474- CPU_ASSERT (tlsPhys != MEMMGR_PHYS_ADDR_ERROR ,
5475- "Failed to get mapped physical address" ,
5476- OS_ERR_INCORRECT_VALUE );
5477-
5478- pTmpData = memoryKernelMap ((void * )tlsPhys ,
5479- size ,
5480- MEMMGR_MAP_KERNEL | MEMMGR_MAP_RW ,
5481- & error );
5482- if (error != OS_NO_ERR )
5461+ while (size > 0 )
54835462 {
5484- intError = memoryUserFree (pThread -> pUserThreadData ,
5485- size ,
5486- pThread -> pProcess );
5487- CPU_ASSERT (intError == OS_NO_ERR ,
5488- "Failed to free allocated memory" ,
5489- OS_ERR_INCORRECT_VALUE );
5490- return error ;
5463+ error = memoryManageCOW (srcTls , srcTlsPhys , pThread );
5464+ if (error != OS_NO_ERR )
5465+ {
5466+ return error ;
5467+ }
5468+ error = memoryManageCOW (srcTls , srcTlsPhys , kpSrcThread );
5469+ if (error != OS_NO_ERR )
5470+ {
5471+ return error ;
5472+ }
5473+ size -= KERNEL_PAGE_SIZE ;
5474+ srcTls += KERNEL_PAGE_SIZE ;
5475+ srcTlsPhys += KERNEL_PAGE_SIZE ;
54915476 }
54925477
5493- /* Get the TLS and copy */
5494- tls = (uintptr_t )kpSrcThread -> pUserThreadData -
5495- ALIGN_UP (pThread -> pProcess -> mainTlsSize , align );
5496- memcpy (pTmpData ,
5497- (void * )tls ,
5498- (uintptr_t )kpSrcThread -> pUserThreadData - tls );
5499-
5500- intError = memoryKernelUnmap (pTmpData , size );
5501- CPU_ASSERT (intError == OS_NO_ERR ,
5502- "Failed to unmap mapped memory" ,
5503- OS_ERR_INCORRECT_VALUE );
5504-
55055478 /* Setup the user data pointer */
5506- pThread -> pUserThreadData = (void * )((uintptr_t )pThread -> pUserThreadData +
5507- ALIGN_UP (pThread -> pProcess -> mainTlsSize , align ));
5508-
5479+ pThread -> pUserThreadData = kpSrcThread -> pUserThreadData ;
5480+
55095481 return OS_NO_ERR ;
55105482}
55115483
0 commit comments