2012-08-07 18:19:44 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-01-11 17:07:45 -05:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2012, 2014.
|
2012-08-07 18:19:44 -04:00
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-01-11 16:45:30 -05:00
|
|
|
copy.cpp
|
2012-08-07 18:19:44 -04:00
|
|
|
The context for io operations: who made it, how should data be copied, etc.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2014-09-22 09:03:37 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-09-22 09:03:37 -04:00
|
|
|
#include <sortix/mman.h>
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/kernel/copy.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/kernel.h>
|
2014-09-22 09:03:37 -04:00
|
|
|
#include <sortix/kernel/kthread.h>
|
|
|
|
#include <sortix/kernel/process.h>
|
|
|
|
#include <sortix/kernel/segment.h>
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/kernel/string.h>
|
|
|
|
|
2014-09-22 09:03:37 -04:00
|
|
|
// NOTE: The copy-from-user-space functions are specially made such that they
|
|
|
|
// guarantee the relevant memory exist and are unchanged during the copy
|
|
|
|
// operation. They do not protect against another thread concurrently
|
|
|
|
// modifying the memory, but the functions cope in that case and just
|
|
|
|
// transfer whatever memory they see in that case, they don't malfunction.
|
|
|
|
|
|
|
|
// TODO: We could check the page tables for extra safely.
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
namespace Sortix {
|
|
|
|
|
2014-09-22 09:03:37 -04:00
|
|
|
static bool IsInProgressAddressSpace(Process* process)
|
|
|
|
{
|
|
|
|
addr_t current_address_space;
|
|
|
|
#if defined(__i386__)
|
|
|
|
asm ( "mov %%cr3, %0" : "=r"(current_address_space) );
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
asm ( "mov %%cr3, %0" : "=r"(current_address_space) );
|
|
|
|
#else
|
|
|
|
#warning "You should set current_address_space for safety"
|
|
|
|
current_address_space = process->addrspace;
|
|
|
|
#endif
|
|
|
|
return current_address_space == process->addrspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct segment* FindSegment(Process* process, uintptr_t addr)
|
|
|
|
{
|
|
|
|
for ( size_t i = 0; i < process->segments_used; i++ )
|
|
|
|
{
|
|
|
|
struct segment* segment = &process->segments[i];
|
|
|
|
if ( addr < segment->addr )
|
|
|
|
continue;
|
|
|
|
if ( segment->addr + segment->size <= addr )
|
|
|
|
continue;
|
|
|
|
return segment;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
|
2014-09-22 09:03:37 -04:00
|
|
|
bool CopyToUser(void* userdst_ptr, const void* ksrc_ptr, size_t count)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2014-09-22 09:03:37 -04:00
|
|
|
uintptr_t userdst = (uintptr_t) userdst_ptr;
|
|
|
|
uintptr_t ksrc = (uintptr_t) ksrc_ptr;
|
|
|
|
bool result = true;
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
assert(IsInProgressAddressSpace(process));
|
|
|
|
kthread_mutex_lock(&process->segment_lock);
|
|
|
|
while ( count )
|
|
|
|
{
|
|
|
|
struct segment* segment = FindSegment(process, userdst);
|
|
|
|
if ( !segment || !(segment->prot & PROT_WRITE) )
|
|
|
|
{
|
|
|
|
errno = EFAULT;
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
size_t amount = count;
|
|
|
|
size_t segment_available = segment->addr + segment->size - userdst;
|
|
|
|
if ( segment_available < amount )
|
|
|
|
amount = segment_available;
|
|
|
|
memcpy((void*) userdst, (const void*) ksrc, amount);
|
|
|
|
userdst += amount;
|
|
|
|
ksrc += amount;
|
|
|
|
count -= amount;
|
|
|
|
}
|
|
|
|
kthread_mutex_unlock(&process->segment_lock);
|
|
|
|
return result;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
2014-09-22 09:03:37 -04:00
|
|
|
bool CopyFromUser(void* kdst_ptr, const void* usersrc_ptr, size_t count)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2014-09-22 09:03:37 -04:00
|
|
|
uintptr_t kdst = (uintptr_t) kdst_ptr;
|
|
|
|
uintptr_t usersrc = (uintptr_t) usersrc_ptr;
|
|
|
|
bool result = true;
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
assert(IsInProgressAddressSpace(process));
|
|
|
|
kthread_mutex_lock(&process->segment_lock);
|
|
|
|
while ( count )
|
|
|
|
{
|
|
|
|
struct segment* segment = FindSegment(process, usersrc);
|
|
|
|
if ( !segment || !(segment->prot & PROT_READ) )
|
|
|
|
{
|
|
|
|
errno = EFAULT;
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
size_t amount = count;
|
|
|
|
size_t segment_available = segment->addr + segment->size - usersrc;
|
|
|
|
if ( segment_available < amount )
|
|
|
|
amount = segment_available;
|
|
|
|
memcpy((void*) kdst, (const void*) usersrc, amount);
|
|
|
|
kdst += amount;
|
|
|
|
usersrc += amount;
|
|
|
|
count -= amount;
|
|
|
|
}
|
|
|
|
kthread_mutex_unlock(&process->segment_lock);
|
|
|
|
return result;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CopyToKernel(void* kdst, const void* ksrc, size_t count)
|
|
|
|
{
|
|
|
|
memcpy(kdst, ksrc, count);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CopyFromKernel(void* kdst, const void* ksrc, size_t count)
|
|
|
|
{
|
|
|
|
memcpy(kdst, ksrc, count);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-11 17:07:45 -05:00
|
|
|
bool ZeroKernel(void* kdst, size_t count)
|
|
|
|
{
|
2014-09-22 09:03:37 -04:00
|
|
|
// TODO: We could check the page tables for extra safely.
|
2014-01-11 17:07:45 -05:00
|
|
|
memset(kdst, 0, count);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-22 09:03:37 -04:00
|
|
|
bool ZeroUser(void* userdst_ptr, size_t count)
|
2014-01-11 17:07:45 -05:00
|
|
|
{
|
2014-09-22 09:03:37 -04:00
|
|
|
uintptr_t userdst = (uintptr_t) userdst_ptr;
|
|
|
|
bool result = true;
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
assert(IsInProgressAddressSpace(process));
|
|
|
|
kthread_mutex_lock(&process->segment_lock);
|
|
|
|
while ( count )
|
|
|
|
{
|
|
|
|
struct segment* segment = FindSegment(process, userdst);
|
|
|
|
if ( !segment || !(segment->prot & PROT_WRITE) )
|
|
|
|
{
|
|
|
|
errno = EFAULT;
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
size_t amount = count;
|
|
|
|
size_t segment_available = segment->addr + segment->size - userdst;
|
|
|
|
if ( segment_available < amount )
|
|
|
|
amount = segment_available;
|
|
|
|
memset((void*) userdst, 0, amount);
|
|
|
|
userdst += amount;
|
|
|
|
count -= amount;
|
|
|
|
}
|
|
|
|
kthread_mutex_unlock(&process->segment_lock);
|
|
|
|
return result;
|
2014-01-11 17:07:45 -05:00
|
|
|
}
|
|
|
|
|
2014-09-22 09:03:37 -04:00
|
|
|
// NOTE: No overflow can happen here because the user can't make an infinitely
|
|
|
|
// long string spanning the entire address space because the user can't
|
|
|
|
// control the entire address space.
|
|
|
|
char* GetStringFromUser(const char* usersrc_str)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2014-09-22 09:03:37 -04:00
|
|
|
uintptr_t usersrc = (uintptr_t) usersrc_str;
|
|
|
|
size_t result_length = 0;
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
assert(IsInProgressAddressSpace(process));
|
|
|
|
|
|
|
|
kthread_mutex_lock(&process->segment_lock);
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
uintptr_t current_at = usersrc + result_length;
|
|
|
|
struct segment* segment = FindSegment(process, current_at);
|
|
|
|
if ( !segment || !(segment->prot & PROT_READ) )
|
|
|
|
{
|
|
|
|
kthread_mutex_unlock(&process->segment_lock);
|
|
|
|
return errno = EFAULT, (char*) NULL;
|
|
|
|
}
|
|
|
|
size_t segment_available = segment->addr + segment->size - current_at;
|
|
|
|
volatile const char* str = (volatile const char*) current_at;
|
|
|
|
size_t length = 0;
|
|
|
|
for ( ; length < segment_available; length++ )
|
|
|
|
{
|
|
|
|
char c = str[length];
|
|
|
|
if ( c == '\0' )
|
|
|
|
break;
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
result_length += length;
|
|
|
|
if ( length < segment_available )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* result = new char[result_length + 1];
|
|
|
|
if ( !result )
|
|
|
|
{
|
|
|
|
kthread_mutex_unlock(&process->segment_lock);
|
|
|
|
return (char*) NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(result, (const char*) usersrc, result_length);
|
|
|
|
result[result_length] = '\0';
|
|
|
|
|
|
|
|
// We have transferred a bunch of bytes from user-space and appended a zero
|
|
|
|
// byte. This is a string. If no concurrent threads were modifying the
|
|
|
|
// memory, this is the intended string. If the memory was modified, we got
|
|
|
|
// potential garbage followed by a NUL byte. This is a string, but probably
|
|
|
|
// not what was intended. If the garbage itself had a premature unexpected
|
|
|
|
// NUL byte, that's okay, the garbage string just got truncated.
|
|
|
|
|
|
|
|
kthread_mutex_unlock(&process->segment_lock);
|
|
|
|
return result;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Sortix
|